📄 abstractbutton.java
字号:
* Set the current keyboard mnemonic value. This value corresponds to a * single key code (one of the {@link java.awt.event.KeyEvent} VK_* * codes) and is used to activate the button when pressed in conjunction * with the "mouseless modifier" of the button's look and feel class, and * when focus is in one of the button's ancestors. * * @param mne A new mnemonic to use for the button */ public void setMnemonic(int mne) { ButtonModel mod = getModel(); int old = -1; if (mod != null) old = mod.getMnemonic(); if (old != mne) { if (mod != null) mod.setMnemonic(mne); if (text != null && !text.equals("")) { // Since lower case char = upper case char for // mnemonic, we will convert both text and mnemonic // to upper case before checking if mnemonic character occurs // in the menu item text. int upperCaseMne = Character.toUpperCase((char) mne); String upperCaseText = text.toUpperCase(); setDisplayedMnemonicIndex(upperCaseText.indexOf(upperCaseMne)); } firePropertyChange(MNEMONIC_CHANGED_PROPERTY, old, mne); revalidate(); repaint(); } } /** * Sets the button's mnemonic index. The mnemonic index is a hint to the * look and feel class, suggesting which character in the button's label * should be underlined when drawing the label. If the mnemonic index is * -1, no mnemonic will be displayed. * * If no mnemonic index is set, the button will choose a mnemonic index * by default, which will be the first occurrence of the mnemonic * character in the button's text. * * @param index An offset into the "text" property of the button * @throws IllegalArgumentException If <code>index</code> is not within the * range of legal offsets for the "text" property of the button. * @since 1.4 */ public void setDisplayedMnemonicIndex(int index) { if (index < -1 || (text != null && index >= text.length())) throw new IllegalArgumentException(); mnemonicIndex = index; } /** * Get the button's mnemonic index, which is an offset into the button's * "text" property. The character specified by this offset should be * underlined when the look and feel class draws this button. * * @return An index into the button's "text" property */ public int getDisplayedMnemonicIndex() { return mnemonicIndex; } /** * Set the "rolloverEnabled" property. When rollover is enabled, and the * look and feel supports it, the button will change its icon to * rolloverIcon, when the mouse passes over it. * * @param r Whether or not to enable rollover icon changes */ public void setRolloverEnabled(boolean r) { if (rollOverEnabled != r) { rollOverEnabled = r; firePropertyChange(ROLLOVER_ENABLED_CHANGED_PROPERTY, !r, r); revalidate(); repaint(); } } /** * Returns whether or not rollover icon changes are enabled on the * button. * * @return The state of the "rolloverEnabled" property */ public boolean isRolloverEnabled() { return rollOverEnabled; } /** * Set the value of the button's "selected" property. Selection is only * meaningful for toggle-type buttons (check boxes, radio buttons). * * @param s New value for the property */ public void setSelected(boolean s) { ButtonModel mod = getModel(); if (mod != null) mod.setSelected(s); } /** * Get the value of the button's "selected" property. Selection is only * meaningful for toggle-type buttons (check boxes, radio buttons). * * @return The value of the property */ public boolean isSelected() { ButtonModel mod = getModel(); if (mod != null) return mod.isSelected(); return false; } /** * Enables or disables the button. A button will neither be selectable * nor preform any actions unless it is enabled. * * @param b Whether or not to enable the button */ public void setEnabled(boolean b) { // Do nothing if state does not change. if (b == isEnabled()) return; super.setEnabled(b); setFocusable(b); ButtonModel mod = getModel(); if (mod != null) mod.setEnabled(b); } /** * Set the horizontal alignment of the button's text and icon. The * alignment is a numeric constant from {@link SwingConstants}. It must * be one of: <code>RIGHT</code>, <code>LEFT</code>, <code>CENTER</code>, * <code>LEADING</code> or <code>TRAILING</code>. The default is * <code>RIGHT</code>. * * @return The current horizontal alignment */ public int getHorizontalAlignment() { return horizontalAlignment; } /** * Set the horizontal alignment of the button's text and icon. The * alignment is a numeric constant from {@link SwingConstants}. It must * be one of: <code>RIGHT</code>, <code>LEFT</code>, <code>CENTER</code>, * <code>LEADING</code> or <code>TRAILING</code>. The default is * <code>RIGHT</code>. * * @param a The new horizontal alignment * @throws IllegalArgumentException If alignment is not one of the legal * constants. */ public void setHorizontalAlignment(int a) { if (horizontalAlignment == a) return; int old = horizontalAlignment; horizontalAlignment = a; firePropertyChange(HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY, old, a); revalidate(); repaint(); } /** * Get the horizontal position of the button's text relative to its * icon. The position is a numeric constant from {@link * SwingConstants}. It must be one of: <code>RIGHT</code>, * <code>LEFT</code>, <code>CENTER</code>, <code>LEADING</code> or * <code>TRAILING</code>. The default is <code>TRAILING</code>. * * @return The current horizontal text position */ public int getHorizontalTextPosition() { return horizontalTextPosition; } /** * Set the horizontal position of the button's text relative to its * icon. The position is a numeric constant from {@link * SwingConstants}. It must be one of: <code>RIGHT</code>, * <code>LEFT</code>, <code>CENTER</code>, <code>LEADING</code> or * <code>TRAILING</code>. The default is <code>TRAILING</code>. * * @param t The new horizontal text position * @throws IllegalArgumentException If position is not one of the legal * constants. */ public void setHorizontalTextPosition(int t) { if (horizontalTextPosition == t) return; int old = horizontalTextPosition; horizontalTextPosition = t; firePropertyChange(HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY, old, t); revalidate(); repaint(); } /** * Get the vertical alignment of the button's text and icon. The * alignment is a numeric constant from {@link SwingConstants}. It must * be one of: <code>CENTER</code>, <code>TOP</code>, or * <code>BOTTOM</code>. The default is <code>CENTER</code>. * * @return The current vertical alignment */ public int getVerticalAlignment() { return verticalAlignment; } /** * Set the vertical alignment of the button's text and icon. The * alignment is a numeric constant from {@link SwingConstants}. It must * be one of: <code>CENTER</code>, <code>TOP</code>, or * <code>BOTTOM</code>. The default is <code>CENTER</code>. * * @param a The new vertical alignment * @throws IllegalArgumentException If alignment is not one of the legal * constants. */ public void setVerticalAlignment(int a) { if (verticalAlignment == a) return; int old = verticalAlignment; verticalAlignment = a; firePropertyChange(VERTICAL_ALIGNMENT_CHANGED_PROPERTY, old, a); revalidate(); repaint(); } /** * Get the vertical position of the button's text relative to its * icon. The alignment is a numeric constant from {@link * SwingConstants}. It must be one of: <code>CENTER</code>, * <code>TOP</code>, or <code>BOTTOM</code>. The default is * <code>CENTER</code>. * * @return The current vertical position */ public int getVerticalTextPosition() { return verticalTextPosition; } /** * Set the vertical position of the button's text relative to its * icon. The alignment is a numeric constant from {@link * SwingConstants}. It must be one of: <code>CENTER</code>, * <code>TOP</code>, or <code>BOTTOM</code>. The default is * <code>CENTER</code>. * * @param t The new vertical position * @throws IllegalArgumentException If position is not one of the legal * constants. */ public void setVerticalTextPosition(int t) { if (verticalTextPosition == t) return; int old = verticalTextPosition; verticalTextPosition = t; firePropertyChange(VERTICAL_TEXT_POSITION_CHANGED_PROPERTY, old, t); revalidate(); repaint(); } /** * Set the value of the "borderPainted" property. If set to * <code>false</code>, the button's look and feel class should not paint * a border for the button. The default is <code>true</code>. * * @return The current value of the property. */ public boolean isBorderPainted() { return borderPainted; } /** * Set the value of the "borderPainted" property. If set to * <code>false</code>, the button's look and feel class should not paint * a border for the button. The default is <code>true</code>. * * @param b The new value of the property. */ public void setBorderPainted(boolean b) { if (borderPainted == b) return; boolean old = borderPainted; borderPainted = b; firePropertyChange(BORDER_PAINTED_CHANGED_PROPERTY, old, b); revalidate(); repaint(); } /** * Get the value of the "action" property. * * @return The current value of the "action" property */ public Action getAction() { return action; } /** * <p>Set the button's "action" property, subscribing the new action to the * button, as an ActionListener, if it is not already subscribed. The old * Action, if it exists, is unsubscribed, and the button is unsubscribed * from the old Action if it was previously subscribed as a * PropertyChangeListener.</p> * * <p>This method also configures several of the button's properties from * the Action, by calling {@link #configurePropertiesFromAction}, and * subscribes the button to the Action as a PropertyChangeListener. * Subsequent changes to the Action will thus reconfigure the button * automatically.</p> * * @param a The new value of the "action" property */ public void setAction(Action a) { if (action != null) { action.removePropertyChangeListener(actionPropertyChangeListener); removeActionListener(action); if (actionPropertyChangeListener != null) { action.removePropertyChangeListener(actionPropertyChangeListener); actionPropertyChangeListener = null; } } Action old = action; action = a; configurePropertiesFromAction(action); if (action != null) { actionPropertyChangeListener = createActionPropertyChangeListener(a); action.addPropertyChangeListener(actionPropertyChangeListener); addActionListener(action); } } /** * Return the button's default "icon" property. * * @return The current default icon */ public Icon getIcon() { return default_icon; } /** * Set the button's default "icon" property. This icon is used as a basis * for the pressed and disabled icons, if none are explicitly set. * * @param i The new default icon */ public void setIcon(Icon i) { if (default_icon == i) return; Icon old = default_icon; default_icon = i; firePropertyChange(ICON_CHANGED_PROPERTY, old, i); revalidate(); repaint(); } /** * Return the button's "text" property. This property is synonymous with * the "label" property. * * @return The current "text" property */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -