📄 abstractbutton.java
字号:
public String getText() { return text; } /** * Set the button's "label" property. This property is synonymous with the * "text" property. * * @param label The new "label" property * * @deprecated use <code>setText(text)</code> */ public void setLabel(String label) { setText(label); } /** * Return the button's "label" property. This property is synonymous with * the "text" property. * * @return The current "label" property * * @deprecated use <code>getText()</code> */ public String getLabel() { return getText(); } /** * Set the button's "text" property. This property is synonymous with the * "label" property. * * @param t The new "text" property */ public void setText(String t) { if (text == t) return; String old = text; text = t; firePropertyChange(TEXT_CHANGED_PROPERTY, old, t); revalidate(); repaint(); } /** * Set the value of the {@link #iconTextGap} property. * * @param i The new value of the property */ public void setIconTextGap(int i) { if (iconTextGap == i) return; int old = iconTextGap; iconTextGap = i; fireStateChanged(); revalidate(); repaint(); } /** * Get the value of the {@link #iconTextGap} property. * * @return The current value of the property */ public int getIconTextGap() { return iconTextGap; } /** * Return the button's "margin" property, which is an {@link Insets} object * describing the distance between the button's border and its text and * icon. * * @return The current "margin" property */ public Insets getMargin() { return margin; } /** * Set the button's "margin" property, which is an {@link Insets} object * describing the distance between the button's border and its text and * icon. * * @param m The new "margin" property */ public void setMargin(Insets m) { if (margin == m) return; Insets old = margin; margin = m; firePropertyChange(MARGIN_CHANGED_PROPERTY, old, m); revalidate(); repaint(); } /** * Return the button's "pressedIcon" property. The look and feel class * should paint this icon when the "pressed" property of the button's * {@link ButtonModel} is <code>true</code>. This property may be * <code>null</code>, in which case the default icon is used. * * @return The current "pressedIcon" property */ public Icon getPressedIcon() { return pressed_icon; } /** * Set the button's "pressedIcon" property. The look and feel class * should paint this icon when the "pressed" property of the button's * {@link ButtonModel} is <code>true</code>. This property may be * <code>null</code>, in which case the default icon is used. * * @param pressedIcon The new "pressedIcon" property */ public void setPressedIcon(Icon pressedIcon) { if (pressed_icon == pressedIcon) return; Icon old = pressed_icon; pressed_icon = pressedIcon; firePropertyChange(PRESSED_ICON_CHANGED_PROPERTY, old, pressed_icon); revalidate(); repaint(); } /** * Return the button's "disabledIcon" property. The look and feel class * should paint this icon when the "enabled" property of the button's * {@link ButtonModel} is <code>false</code>. This property may be * <code>null</code>, in which case an icon is constructed, based on the * default icon. * * @return The current "disabledIcon" property */ public Icon getDisabledIcon() { if (disabeldIcon == null && default_icon instanceof ImageIcon) { Image iconImage = ((ImageIcon) default_icon).getImage(); Image grayImage = GrayFilter.createDisabledImage(iconImage); disabeldIcon = new ImageIcon(grayImage); } return disabeldIcon; } /** * Set the button's "disabledIcon" property. The look and feel class should * paint this icon when the "enabled" property of the button's {@link * ButtonModel} is <code>false</code>. This property may be * <code>null</code>, in which case an icon is constructed, based on the * default icon. * * @param d The new "disabledIcon" property */ public void setDisabledIcon(Icon d) { disabeldIcon = d; revalidate(); repaint(); } /** * Return the button's "paintFocus" property. This property controls * whether or not the look and feel class will paint a special indicator * of focus state for the button. If it is false, the button still paints * when focused, but no special decoration is painted to indicate the * presence of focus. * * @return The current "paintFocus" property */ public boolean isFocusPainted() { return focusPainted; } /** * Set the button's "paintFocus" property. This property controls whether * or not the look and feel class will paint a special indicator of focus * state for the button. If it is false, the button still paints when * focused, but no special decoration is painted to indicate the presence * of focus. * * @param p The new "paintFocus" property */ public void setFocusPainted(boolean p) { if (focusPainted == p) return; boolean old = focusPainted; focusPainted = p; firePropertyChange(FOCUS_PAINTED_CHANGED_PROPERTY, old, p); revalidate(); repaint(); } /** * Verifies that a particular key is one of the valid constants used for * describing horizontal alignment and positioning. The valid constants * are the following members of {@link SwingConstants}: * <code>RIGHT</code>, <code>LEFT</code>, <code>CENTER</code>, * <code>LEADING</code> or <code>TRAILING</code>. * * @param key The key to check * @param exception A message to include in an IllegalArgumentException * * @return the value of key * * @throws IllegalArgumentException If key is not one of the valid constants * * @see #setHorizontalTextPosition(int) * @see #setHorizontalAlignment(int) */ protected int checkHorizontalKey(int key, String exception) { switch (key) { case SwingConstants.RIGHT: case SwingConstants.LEFT: case SwingConstants.CENTER: case SwingConstants.LEADING: case SwingConstants.TRAILING: break; default: throw new IllegalArgumentException(exception); } return key; } /** * Verifies that a particular key is one of the valid constants used for * describing vertical alignment and positioning. The valid constants are * the following members of {@link SwingConstants}: <code>TOP</code>, * <code>BOTTOM</code> or <code>CENTER</code>. * * @param key The key to check * @param exception A message to include in an IllegalArgumentException * * @return the value of key * * @throws IllegalArgumentException If key is not one of the valid constants * * @see #setVerticalTextPosition(int) * @see #setVerticalAlignment(int) */ protected int checkVerticalKey(int key, String exception) { switch (key) { case SwingConstants.TOP: case SwingConstants.BOTTOM: case SwingConstants.CENTER: break; default: throw new IllegalArgumentException(exception); } return key; } /** * Configure various properties of the button by reading properties * of an {@link Action}. The mapping of properties is as follows: * * <table> * * <tr><th>Action keyed property</th> <th>AbstractButton property</th></tr> * * <tr><td>NAME </td> <td>text </td></tr> * <tr><td>SMALL_ICON </td> <td>icon </td></tr> * <tr><td>SHORT_DESCRIPTION </td> <td>toolTipText </td></tr> * <tr><td>MNEMONIC_KEY </td> <td>mnemonic </td></tr> * <tr><td>ACTION_COMMAND_KEY </td> <td>actionCommand </td></tr> * * </table> * * <p>In addition, this method always sets the button's "enabled" property to * the value of the Action's "enabled" property.</p> * * <p>If the provided Action is <code>null</code>, the text, icon, and * toolTipText properties of the button are set to <code>null</code>, and * the "enabled" property is set to <code>true</code>; the mnemonic and * actionCommand properties are unchanged.</p> * * @param a An Action to configure the button from */ protected void configurePropertiesFromAction(Action a) { if (a == null) { setText(null); setIcon(null); setEnabled(true); setToolTipText(null); } else { setText((String) (a.getValue(Action.NAME))); setIcon((Icon) (a.getValue(Action.SMALL_ICON))); setEnabled(a.isEnabled()); setToolTipText((String) (a.getValue(Action.SHORT_DESCRIPTION))); if (a.getValue(Action.MNEMONIC_KEY) != null) setMnemonic(((Integer) (a.getValue(Action.MNEMONIC_KEY))).intValue()); String actionCommand = (String) (a.getValue(Action.ACTION_COMMAND_KEY)); // Set actionCommand to button's text by default if it is not specified if (actionCommand != null) setActionCommand((String) (a.getValue(Action.ACTION_COMMAND_KEY))); else setActionCommand(getText()); } } /** * <p>A factory method which should return an {@link ActionListener} that * propagates events from the button's {@link ButtonModel} to any of the * button's ActionListeners. By default, this is an inner class which * calls {@link AbstractButton#fireActionPerformed} with a modified copy * of the incoming model {@link ActionEvent}.</p> * * <p>The button calls this method during construction, stores the * resulting ActionListener in its <code>actionListener</code> member * field, and subscribes it to the button's model. If the button's model * is changed, this listener is unsubscribed from the old model and * subscribed to the new one.</p> * * @return A new ActionListener */ protected ActionListener createActionListener() { return new ActionListener() { public void actionPerformed(ActionEvent e) { AbstractButton.this.fireActionPerformed(e); } }; } /** * <p>A factory method which should return a {@link PropertyChangeListener} * that accepts changes to the specified {@link Action} and reconfigure * the {@link AbstractButton}, by default using the {@link * #configurePropertiesFromAction} method.</p> * * <p>The button calls this method whenever a new Action is assigned to * the button's "action" property, via {@link #setAction}, and stores the * resulting PropertyChangeListener in its * <code>actionPropertyChangeListener</code> member field. The button * then subscribes the listener to the button's new action. If the * button's action is changed subsequently, the listener is unsubscribed * from the old action and subscribed to the new one.</p> * * @param a The Action which will be listened to, and which should be * the same as the source of any PropertyChangeEvents received by the * new listener returned from this method. * * @return A new PropertyChangeListener */ protected PropertyChangeListener createActionPropertyChangeListener(Action a) { return new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { Action act = (Action) (e.getSource()); if (e.getPropertyName().equals("enabled")) setEnabled(act.isEnabled()); else if (e.getPropertyName().equals(Action.NAME)) setText((String) (act.getValue(Action.NAME))); else if (e.getPropertyName().equals(Action.SMALL_ICON)) setIcon((Icon) (act.getValue(Action.SMALL_ICON))); else if (e.getPropertyName().equals(Action.SHORT_DESCRIPTION)) setToolTipText((String) (act.getValue(Action.SHORT_DESCRIPTION))); else if (e.getPropertyName().equals(Action.MNEMONIC_KEY)) if (act.getValue(Action.MNEMONIC_KEY) != null) setMnemonic(((Integer) (act.getValue(Action.MNEMONIC_KEY))) .intValue()); else if (e.getPropertyName().equals(Action.ACTION_COMMAND_KEY)) setActionCommand((String) (act.getValue(Action.ACTION_COMMAND_KEY))); } }; } /** * <p>Factory method which creates a {@link ChangeListener}, used to * subscribe to ChangeEvents from the button's model. Subclasses of * AbstractButton may wish to override the listener used to subscribe to * such ChangeEvents. By default, the listener just propagates the * {@link ChangeEvent} to the button's ChangeListeners, via the {@link * AbstractButton#fireStateChanged} method.</p> * * <p>The button calls this method during construction, stores the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -