📄 abstractbutton.java
字号:
/* AbstractButton.java -- Provides basic button functionality. Copyright (C) 2002, 2004 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing;import java.awt.Graphics;import java.awt.Image;import java.awt.Insets;import java.awt.ItemSelectable;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.image.ImageObserver;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.io.Serializable;import javax.accessibility.AccessibleAction;import javax.accessibility.AccessibleIcon;import javax.accessibility.AccessibleRelationSet;import javax.accessibility.AccessibleStateSet;import javax.accessibility.AccessibleText;import javax.accessibility.AccessibleValue;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.plaf.ButtonUI;import javax.swing.text.AttributeSet;/** * Provides an abstract implementation of common button behaviour, * data model and look & feel. * * <p>This class is supposed to serve as a base class for * several kinds of buttons with similar but non-identical semantics: * toggle buttons (radio buttons and checkboxes), simple push buttons, * menu items, etc.</p> * * <p>Buttons have many properties, some of which are stored in this class * while others are delegated to the button's model. The following properties * are available:</p> * * <table> * <tr><th>Property </th><th>Stored in</th><th>Bound?</th></tr> * * <tr><td>action </td><td>button</td> <td>no</td></tr> * <tr><td>actionCommand </td><td>model</td> <td>no</td></tr> * <tr><td>borderPainted </td><td>button</td> <td>yes</td></tr> * <tr><td>contentAreaFilled </td><td>button</td> <td>yes</td></tr> * <tr><td>disabledIcon </td><td>button</td> <td>yes</td></tr> * <tr><td>disabledSelectedIcon </td><td>button</td> <td>yes</td></tr> * <tr><td>displayedMnemonicIndex </td><td>button</td> <td>no</td></tr> * <tr><td>enabled </td><td>model</td> <td>no</td></tr> * <tr><td>focusPainted </td><td>button</td> <td>yes</td></tr> * <tr><td>horizontalAlignment </td><td>button</td> <td>yes</td></tr> * <tr><td>horizontalTextPosition </td><td>button</td> <td>yes</td></tr> * <tr><td>icon </td><td>button</td> <td>yes</td></tr> * <tr><td>iconTextGap </td><td>button</td> <td>no</td></tr> * <tr><td>label (same as text) </td><td>model</td> <td>yes</td></tr> * <tr><td>margin </td><td>button</td> <td>yes</td></tr> * <tr><td>multiClickThreshold </td><td>button</td> <td>no</td></tr> * <tr><td>pressedIcon </td><td>button</td> <td>yes</td></tr> * <tr><td>rolloverEnabled </td><td>button</td> <td>yes</td></tr> * <tr><td>rolloverIcon </td><td>button</td> <td>yes</td></tr> * <tr><td>rolloverSelectedIcon </td><td>button</td> <td>yes</td></tr> * <tr><td>selected </td><td>model</td> <td>no</td></tr> * <tr><td>selectedIcon </td><td>button</td> <td>yes</td></tr> * <tr><td>selectedObjects </td><td>button</td> <td>no</td></tr> * <tr><td>text </td><td>model</td> <td>yes</td></tr> * <tr><td>UI </td><td>button</td> <td>yes</td></tr> * <tr><td>verticalAlignment </td><td>button</td> <td>yes</td></tr> * <tr><td>verticalTextPosition </td><td>button</td> <td>yes</td></tr> * * </table> * * <p>The various behavioral aspects of these properties follows:</p> * * <ul> * * <li>When non-bound properties stored in the button change, the button * fires ChangeEvents to its ChangeListeners.</li> * * <li>When bound properties stored in the button change, the button fires * PropertyChangeEvents to its PropertyChangeListeners</li> * * <li>If any of the model's properties change, it fires a ChangeEvent to * its ChangeListeners, which include the button.</li> * * <li>If the button receives a ChangeEvent from its model, it will * propagate the ChangeEvent to its ChangeListeners, with the ChangeEvent's * "source" property set to refer to the button, rather than the model. The * the button will request a repaint, to paint its updated state.</li> * * <li>If the model's "selected" property changes, the model will fire an * ItemEvent to its ItemListeners, which include the button, in addition to * the ChangeEvent which models the property change. The button propagates * ItemEvents directly to its ItemListeners.</li> * * <li>If the model's armed and pressed properties are simultaneously * <code>true</code>, the model will fire an ActionEvent to its * ActionListeners, which include the button. The button will propagate * this ActionEvent to its ActionListeners, with the ActionEvent's "source" * property set to refer to the button, rather than the model.</li> * * </ul> * * @author Ronald Veldema (rveldema@cs.vu.nl) * @author Graydon Hoare (graydon@redhat.com) */public abstract class AbstractButton extends JComponent implements ItemSelectable, SwingConstants{ private static final long serialVersionUID = -937921345538462020L; /** * An extension of ChangeListener to be serializable. */ protected class ButtonChangeListener implements ChangeListener, Serializable { private static final long serialVersionUID = 1471056094226600578L; /** * Notified when the target of the listener changes its state. * * @param ev the ChangeEvent describing the change */ public void stateChanged(ChangeEvent ev) { AbstractButton.this.fireStateChanged(); repaint(); } } /** The icon displayed by default. */ Icon default_icon; /** The icon displayed when the button is pressed. */ Icon pressed_icon; /** The icon displayed when the button is disabled. */ Icon disabeldIcon; /** The icon displayed when the button is selected. */ Icon selectedIcon; /** The icon displayed when the button is selected but disabled. */ Icon disabledSelectedIcon; /** The icon displayed when the button is rolled over. */ Icon rolloverIcon; /** The icon displayed when the button is selected and rolled over. */ Icon rolloverSelectedIcon; /** The icon currently displayed. */ Icon current_icon; /** The text displayed in the button. */ String text; /** * The gap between icon and text, if both icon and text are * non-<code>null</code>. */ int iconTextGap; /** The vertical alignment of the button's text and icon. */ int verticalAlignment; /** The horizontal alignment of the button's text and icon. */ int horizontalAlignment; /** The horizontal position of the button's text relative to its icon. */ int horizontalTextPosition; /** The vertical position of the button's text relative to its icon. */ int verticalTextPosition; /** Whether or not the button paints its border. */ boolean borderPainted; /** Whether or not the button paints its focus state. */ boolean focusPainted; /** Whether or not the button fills its content area. */ boolean contentAreaFilled; /** Whether rollover is enabled. */ boolean rollOverEnabled; /** The action taken when the button is clicked. */ Action action; /** The button's current state. */ protected ButtonModel model; /** The margin between the button's border and its label. */ Insets margin; /** * A hint to the look and feel class, suggesting which character in the * button's label should be underlined when drawing the label. */ int mnemonicIndex; /** Listener the button uses to receive ActionEvents from its model. */ protected ActionListener actionListener; /** Listener the button uses to receive ItemEvents from its model. */ protected ItemListener itemListener; /** Listener the button uses to receive ChangeEvents from its model. */ protected ChangeListener changeListener; /** * The time in miliseconds in which clicks get coalesced into a single * <code>ActionEvent</code>. */ long multiClickThreshhold; /** * Listener the button uses to receive PropertyChangeEvents from its * Action. */ PropertyChangeListener actionPropertyChangeListener; /** ChangeEvent that is fired to button's ChangeEventListeners */ protected ChangeEvent changeEvent = new ChangeEvent(this); /** * Fired in a PropertyChangeEvent when the "borderPainted" property changes. */ public static final String BORDER_PAINTED_CHANGED_PROPERTY = "borderPainted"; /** * Fired in a PropertyChangeEvent when the "contentAreaFilled" property * changes. */ public static final String CONTENT_AREA_FILLED_CHANGED_PROPERTY = "contentAreaFilled"; /** * Fired in a PropertyChangeEvent when the "disabledIcon" property changes. */ public static final String DISABLED_ICON_CHANGED_PROPERTY = "disabledIcon"; /** * Fired in a PropertyChangeEvent when the "disabledSelectedIcon" property * changes. */ public static final String DISABLED_SELECTED_ICON_CHANGED_PROPERTY = "disabledSelectedIcon"; /** * Fired in a PropertyChangeEvent when the "focusPainted" property changes. */ public static final String FOCUS_PAINTED_CHANGED_PROPERTY = "focusPainted"; /** * Fired in a PropertyChangeEvent when the "horizontalAlignment" property * changes. */ public static final String HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY = "horizontalAlignment"; /** * Fired in a PropertyChangeEvent when the "horizontalTextPosition" property * changes. */ public static final String HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY = "horizontalTextPosition"; /** * Fired in a PropertyChangeEvent when the "icon" property changes. */ public static final String ICON_CHANGED_PROPERTY = "icon"; /** Fired in a PropertyChangeEvent when the "margin" property changes. */ public static final String MARGIN_CHANGED_PROPERTY = "margin"; /** Fired in a PropertyChangeEvent when the "mnemonic" property changes. */ public static final String MNEMONIC_CHANGED_PROPERTY = "mnemonic"; /** Fired in a PropertyChangeEvent when the "model" property changes. */ public static final String MODEL_CHANGED_PROPERTY = "model"; /** Fired in a PropertyChangeEvent when the "pressedIcon" property changes. */ public static final String PRESSED_ICON_CHANGED_PROPERTY = "pressedIcon"; /** * Fired in a PropertyChangeEvent when the "rolloverEnabled" property * changes. */ public static final String ROLLOVER_ENABLED_CHANGED_PROPERTY = "rolloverEnabled"; /** * Fired in a PropertyChangeEvent when the "rolloverIcon" property changes. */ public static final String ROLLOVER_ICON_CHANGED_PROPERTY = "rolloverIcon"; /** * Fired in a PropertyChangeEvent when the "rolloverSelectedIcon" property * changes. */ public static final String ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY = "rolloverSelectedIcon"; /** * Fired in a PropertyChangeEvent when the "selectedIcon" property changes. */ public static final String SELECTED_ICON_CHANGED_PROPERTY = "selectedIcon"; /** Fired in a PropertyChangeEvent when the "text" property changes. */ public static final String TEXT_CHANGED_PROPERTY = "text"; /** * Fired in a PropertyChangeEvent when the "verticalAlignment" property * changes. */ public static final String VERTICAL_ALIGNMENT_CHANGED_PROPERTY = "verticalAlignment"; /** * Fired in a PropertyChangeEvent when the "verticalTextPosition" property * changes. */ public static final String VERTICAL_TEXT_POSITION_CHANGED_PROPERTY = "verticalTextPosition"; /** * A Java Accessibility extension of the AbstractButton. */ protected abstract class AccessibleAbstractButton extends AccessibleJComponent implements AccessibleAction, AccessibleValue, AccessibleText { private static final long serialVersionUID = -5673062525319836790L; protected AccessibleAbstractButton() { // Nothing to do here yet. } public AccessibleStateSet getAccessibleStateSet() { return null; // TODO } public String getAccessibleName() { return null; // TODO } public AccessibleIcon[] getAccessibleIcon() { return null; // TODO } public AccessibleRelationSet getAccessibleRelationSet() { return null; // TODO } public AccessibleAction getAccessibleAction() { return null; // TODO } public AccessibleValue getAccessibleValue()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -