下面列出了javax.swing.JButton#putClientProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates the button.
*
* @param a the a
* @return the j button
*/
public JButton createButton(Action a) {
JButton b = new JButton() {
public Dimension getMaximumSize() {
int width = Short.MAX_VALUE;
int height = super.getMaximumSize().height;
return new Dimension(width, height);
}
};
// setting the following client property informs the button to show
// the action text as it's name. The default is to not show the
// action text.
b.putClientProperty("displayActionText", Boolean.TRUE);
b.setAction(a);
// b.setAlignmentX(JButton.CENTER_ALIGNMENT);
return b;
}
public static Component getToolbarPresenter(Action action) {
JButton button = new JButton(action);
button.setBorderPainted(false);
button.setOpaque(false);
button.setText(null);
button.putClientProperty("hideActionText", Boolean.TRUE); // NOI18N
Object icon = action.getValue(Action.SMALL_ICON);
if (icon == null) {
icon = ImageUtilities.loadImageIcon("org/netbeans/modules/dlight/terminal/action/local_term.png", false);// NOI18N
}
if (!(icon instanceof Icon)) {
throw new IllegalStateException("No icon provided for " + action); // NOI18N
}
button.setDisabledIcon(ImageUtilities.createDisabledIcon((Icon) icon));
return button;
}
/**
* This returns a <code>JButton</code> that can be used to trigger help.
* <p>
* Currently this is a circular button with the text "?".
*
* @param actionListener
* the listener that is triggered when you either click the help
* JButton or JLink.
* @param tooltipText
* the optional tooltip text for this component.
* @return the help button
*/
public static JButton create(ActionListener actionListener,
String tooltipText) {
JButton helpButton = new JButton();
QButtonUI ui = new RoundRectButtonUI();
helpButton
.putClientProperty(QButtonUI.PROPERTY_IS_CIRCLE, Boolean.TRUE);
Font font = helpButton.getFont().deriveFont(Font.BOLD);
Icon glyphIcon = new GlyphIcon(font, '?', 14, Color.black);
helpButton.setIcon(glyphIcon);
helpButton.setUI(ui);
helpButton.addActionListener(actionListener);
helpButton.setToolTipText(tooltipText);
return helpButton;
}
private void initButtonPanel() {
buttonPanel.setLayout(new GridLayout(0, 1, 20, 20));
mainButtonPanel.setLayout(new BorderLayout());
mainButtonPanel.add(buttonPanel, BorderLayout.NORTH);
JButton btExit = new JButton(Labels.getString("main-panel-exit-button"));
btExit.addActionListener(e -> settlersFrame.exit());
btExit.putClientProperty(ELFStyle.KEY, ELFStyle.BUTTON_MENU);
mainButtonPanel.add(btExit, BorderLayout.SOUTH);
add(mainButtonPanel);
add(emptyPanel);
getTitleLabel().setVisible(false);
}
/**
* Creates the button.
*
* @param a the a
* @return the j button
*/
public JButton createButton(Action a) {
JButton b = new JButton();
// setting the following client property informs the button to show
// the action text as it's name. The default is to not show the
// action text.
b.putClientProperty("displayActionText", Boolean.TRUE);
b.setAction(a);
return b;
}
@Override
public Component getToolbarPresenter() {
if (popup != null) {
JButton button = DropDownButtonFactory.createDropDownButton(
(ImageIcon) getValue(SMALL_ICON),
popup
);
button.putClientProperty("hideActionText", Boolean.TRUE); //NOI18N
button.setAction(this);
return button;
} else {
return new JButton(this);
}
}
/**
* Test whether pressed, rollover and disabled 24x24 icons
* work for javax.swing.Action.
*/
public void testIconsAction24() throws Exception {
JButton jb = new JButton();
jb.putClientProperty("PreferredIconSize",new Integer(24));
Actions.connect(jb, new TestAction());
Icon icon = jb.getIcon();
assertNotNull(icon);
checkIfLoadedCorrectIcon(icon, jb, 4, "Enabled icon24");
Icon rolloverIcon = jb.getRolloverIcon();
assertNotNull(rolloverIcon);
checkIfLoadedCorrectIcon(rolloverIcon, jb, 5, "Rollover icon24");
Icon pressedIcon = jb.getPressedIcon();
assertNotNull(pressedIcon);
checkIfLoadedCorrectIcon(pressedIcon, jb, 6, "Pressed icon24");
Icon disabledIcon = jb.getDisabledIcon();
assertNotNull(disabledIcon);
checkIfLoadedCorrectIcon(disabledIcon, jb, 7, "Disabled icon24");
Icon selectedIcon = jb.getSelectedIcon();
assertNotNull(selectedIcon);
checkIfLoadedCorrectIcon(selectedIcon, jb, 12, "Selected icon24");
Icon rolloverSelectedIcon = jb.getRolloverSelectedIcon();
assertNotNull(rolloverSelectedIcon);
checkIfLoadedCorrectIcon(rolloverSelectedIcon, jb, 13, "RolloverSelected icon24");
// no pressedSelected
Icon disabledSelectedIcon = jb.getDisabledSelectedIcon();
assertNotNull(disabledSelectedIcon);
checkIfLoadedCorrectIcon(disabledSelectedIcon, jb, 15, "DisabledSelected icon24");
}
private JButton buildNumberButton(int number) {
JButton button = new JButton(Integer.toString(number));
button.setName(""+number);
button.putClientProperty(NUMBER_PROPERTY, Integer.valueOf(number));
button.addActionListener(numberListener);
return button;
}
private JButton adjustButton(JButton b) {
b.setBorderPainted(false);
b.setOpaque(false);
b.setText(null);
b.putClientProperty("hideActionText", Boolean.TRUE); // NOI18N
// NOI18N
return b;
}
@Override
public Component getToolbarPresenter() {
if (popupMenu != null) {
JButton button = DropDownButtonFactory.createDropDownButton(
(ImageIcon) getValue(SMALL_ICON),
popupMenu
);
button.putClientProperty("hideActionText", Boolean.TRUE); //NOI18N
button.setAction(this);
return button;
} else {
return new JButton(this);
}
}
/**
* Pass the name of an application as the argument, and this main() method
* prompts the user with a dialog asking them if they want to abort whatever
* that application is currently doing.
*
* @param args
* the application's arguments. (This is unused.)
*/
public static void main(String[] args) {
if (args == null || args.length == 0 || args[0].trim().length() == 0)
args = new String[] { "Unknown" };
try {
String lf = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(lf);
} catch (Throwable e) {
e.printStackTrace();
}
String name = args[0];
JButton abortButton = new JButton("Abort");
abortButton.putClientProperty(DialogFooter.PROPERTY_UNSAFE,
Boolean.TRUE);
DialogFooter footer = new DialogFooter(new JComponent[] {},
new JComponent[] { new JButton("Cancel"), abortButton }, true,
null);
int i = QDialog
.showDialog(new JFrame(),
"Unresponsive Application", // dialogTitle
QDialog.getIcon(QDialog.WARNING_MESSAGE), // icon
QDialog.createContentPanel(
"The application \""
+ name
+ "\" appears to be unresponsive. Would you like to try to abort its current activity?",
"This may result in data loss, but it may temporarily restore control of the application. If this works, you should immediately try to save your work and exit \""
+ name + "\".", null, // innerComponent
true), // selectable
footer, false, // closeable
null, // dontShowKey
null); // alwaysApplyKey
if (i == 1) {
System.out.println("abort");
} else {
System.out.println("ignore");
}
System.exit(0);
}
/**
* Creates a new "Don't Save" button.
*
* @param escapeKeyIsTrigger
* if true then pressing the escape key will trigger this button.
* (Also on Macs command-period will act like the escape key.)
* This should be <code>false</code> if this button can lead to
* permanent data loss.
*/
public static JButton createDontSaveButton(boolean escapeKeyIsTrigger) {
String text = strings.getString("dialogDontSaveButton");
JButton button = new JButton(text);
button.setMnemonic(strings.getString("dialogDontSaveMnemonic")
.charAt(0));
button.putClientProperty(PROPERTY_OPTION, new Integer(DONT_SAVE_OPTION));
// Don't know if this documented by Apple, but command-D usually
// triggers "Don't Save" buttons:
button.putClientProperty(DialogFooter.PROPERTY_META_SHORTCUT,
new Character(text.charAt(0)));
if (escapeKeyIsTrigger)
makeEscapeKeyActivate(button);
return button;
}
private JButton buildOperatorButton(String symbol, int opType) {
JButton plus = new JButton(symbol);
plus.setName(""+symbol);
plus.putClientProperty(OPERATOR_PROPERTY, Integer.valueOf(opType));
plus.addActionListener(operatorListener);
return plus;
}
private SecurePasswordInputPanel() {
super(new GridLayout(0, 1));
this.passwordField = new JPasswordField(10);
passwordField.setEditable(false);
add(passwordField);
Set<Integer> rows = new HashSet<Integer>(KEYS_MAP.keySet());
final int n = rows.size();
SecureRandom random = new SecureRandom();
while (!rows.isEmpty()) {
int row = random.nextInt() % n;
if (!rows.contains(row)) {
continue;
}
String[] keys = KEYS_MAP.get(row);
rows.remove(row);
JPanel panel = new JPanel();
for (int column = 0; column < keys.length; column++) {
String text = keys[column];
JButton button = new JButton(text);
button.setFont(button.getFont().deriveFont(Font.TRUETYPE_FONT));
if (CLEAR.equalsIgnoreCase(text)) {
button.setBackground(Color.red);
} else if (CAPS.equalsIgnoreCase(text) || BACKSPACE.equalsIgnoreCase(text)) {
button.setBackground(Color.lightGray);
} else {
buttons.add(button);
}
button.putClientProperty("key", text);
button.addActionListener(e -> {
JButton btn = (JButton) e.getSource();
String pressedKey = (String) btn.getClientProperty("key");
if (CAPS.equals(pressedKey)) {
for (JButton m : buttons) {
String txt = m.getText();
m.setText(caps ? txt.toLowerCase() : txt.toUpperCase());
}
caps = !caps;
return;
}
if (BACKSPACE.equals(pressedKey)) {
if (password.length() > 0) {
password = password.substring(0, password.length() - 1);
}
} else if (CLEAR.equals(pressedKey)) {
password = "";
} else {
password += btn.getText();
}
passwordField.setText(password);
});
panel.add(button);
} // end for
add(panel);
} // end while(!rows.isEmpty())
//setVisible(true);
}
private static QOptionPane createReviewChangesDialog(String appName,
int documentCount) {
String mainMessage, comment, screenshot;
Integer textWidth = null;
int iconType;
if (JVM.isWindowsXP) {
iconType = QOptionPane.ICON_WARNING;
} else if (JVM.isWindows) {
iconType = QOptionPane.ICON_NONE;
} else {
iconType = QOptionPane.ICON_QUESTION;
}
JButton reviewChangesButton = new JButton(
strings.getString("dialogReviewChangesButton"));
JButton discardChangesButton = new JButton(
strings.getString("dialogDiscardChangesButton"));
JButton cancelButton = DialogFooter.createCancelButton(true);
JButton[] actionButtons;
if (JVM.isMac) {
actionButtons = new JButton[] { reviewChangesButton, cancelButton,
discardChangesButton };
} else {
actionButtons = new JButton[] { reviewChangesButton,
discardChangesButton, cancelButton };
}
DialogFooter.setUnsafe(discardChangesButton, true);
reviewChangesButton.putClientProperty(DialogFooter.PROPERTY_OPTION,
new Integer(REVIEW_CHANGES_OPTION));
discardChangesButton.putClientProperty(DialogFooter.PROPERTY_OPTION,
new Integer(DISCARD_CHANGES_OPTION));
DialogFooter footer = new DialogFooter(null, actionButtons, true,
reviewChangesButton);
mainMessage = strings.getString("dialogMacMultipleUnsavedMessage");
mainMessage = mainMessage
.replace("^0", Integer.toString(documentCount));
mainMessage = mainMessage.replace("^1", appName);
comment = strings.getString("dialogMacMultipleUnsavedComment");
screenshot = "save_mac_multiple.png";
textWidth = new Integer(480);
QOptionPane pane = new QOptionPane(mainMessage, comment, iconType);
pane.putClientProperty(BasicQOptionPaneUI.KEY_MESSAGE_WIDTH, textWidth);
pane.setDialogTitle(appName);
if (debugWithScreenshots) {
try {
File dir = new File(System.getProperty("user.dir"));
File screenshotFile = FileTreeIterator.find(dir, screenshot);
BufferedImage img = ImageIO.read(screenshotFile);
pane.putClientProperty("debug.ghost.image", img);
} catch (IOException e) {
RuntimeException e2 = new RuntimeException();
e2.initCause(e);
throw e2;
}
}
pane.setDialogFooter(footer);
return pane;
}
/**
* Create swing button from navigation button.
*
* @param navigationButton the navigation button
* @param iconSize the icon size (in case it is 0 default icon size will be used)
* @param specialCallback the special callback
* @return the swing button
*/
public static JButton createSwingButton(
@Nonnull final NavigationButton navigationButton,
int iconSize,
@Nullable final BiConsumer<NavigationButton, JButton> specialCallback)
{
final BufferedImage scaledImage = iconSize > 0
? ImageUtil.resizeImage(navigationButton.getIcon(), iconSize, iconSize)
: navigationButton.getIcon();
final JButton button = new JButton();
button.setSize(scaledImage.getWidth(), scaledImage.getHeight());
button.setToolTipText(navigationButton.getTooltip());
button.setIcon(new ImageIcon(scaledImage));
button.putClientProperty(SubstanceSynapse.FLAT_LOOK, Boolean.TRUE);
button.setFocusable(false);
button.addActionListener(e ->
{
if (specialCallback != null)
{
specialCallback.accept(navigationButton, button);
}
if (navigationButton.getOnClick() != null)
{
navigationButton.getOnClick().run();
}
});
if (navigationButton.getPopup() != null)
{
final JPopupMenu popupMenu = new JPopupMenu();
navigationButton.getPopup().forEach((name, callback) ->
{
final JMenuItem menuItem = new JMenuItem(name);
menuItem.addActionListener((e) -> callback.run());
popupMenu.add(menuItem);
});
button.setComponentPopupMenu(popupMenu);
}
navigationButton.setOnSelect(button::doClick);
return button;
}
/**
* Creates a new "OK" button.
*
* @param escapeKeyIsTrigger
* if true then pressing the escape key will trigger this button.
* (Also on Macs command-period will act like the escape key.)
* This should be <code>false</code> if this button can lead to
* permanent data loss.
*/
public static JButton createOKButton(boolean escapeKeyIsTrigger) {
JButton button = new JButton(strings.getString("dialogOKButton"));
button.setMnemonic(strings.getString("dialogOKMnemonic").charAt(0));
button.putClientProperty(PROPERTY_OPTION, new Integer(OK_OPTION));
if (escapeKeyIsTrigger)
makeEscapeKeyActivate(button);
return button;
}
/**
* Creates a new "Yes" button.
*
* @param escapeKeyIsTrigger
* if true then pressing the escape key will trigger this button.
* (Also on Macs command-period will act like the escape key.)
* This should be <code>false</code> if this button can lead to
* permanent data loss.
*/
public static JButton createYesButton(boolean escapeKeyIsTrigger) {
JButton button = new JButton(strings.getString("dialogYesButton"));
button.setMnemonic(strings.getString("dialogYesMnemonic").charAt(0));
button.putClientProperty(PROPERTY_OPTION, new Integer(YES_OPTION));
if (escapeKeyIsTrigger)
makeEscapeKeyActivate(button);
return button;
}
/**
* Creates a new "No" button.
*
* @param escapeKeyIsTrigger
* if true then pressing the escape key will trigger this button.
* (Also on Macs command-period will act like the escape key.)
* This should be <code>false</code> if this button can lead to
* permanent data loss.
*/
public static JButton createNoButton(boolean escapeKeyIsTrigger) {
JButton button = new JButton(strings.getString("dialogNoButton"));
button.setMnemonic(strings.getString("dialogNoMnemonic").charAt(0));
button.putClientProperty(PROPERTY_OPTION, new Integer(NO_OPTION));
if (escapeKeyIsTrigger)
makeEscapeKeyActivate(button);
return button;
}
/**
* Creates a new "Save" button.
*
* @param escapeKeyIsTrigger
* if true then pressing the escape key will trigger this button.
* (Also on Macs command-period will act like the escape key.)
* This should be <code>false</code> if this button can lead to
* permanent data loss.
*/
public static JButton createSaveButton(boolean escapeKeyIsTrigger) {
JButton button = new JButton(strings.getString("dialogSaveButton"));
button.setMnemonic(strings.getString("dialogSaveMnemonic").charAt(0));
button.putClientProperty(PROPERTY_OPTION, new Integer(SAVE_OPTION));
if (escapeKeyIsTrigger)
makeEscapeKeyActivate(button);
return button;
}