abstractbutton.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,946 行 · 第 1/4 页

JAVA
1,946
字号
        model.removeActionListener(actionListener);        model.removeChangeListener(changeListener);        model.removeItemListener(itemListener);      }    ButtonModel old = model;    model = newModel;    if (model != null)      {        model.addActionListener(actionListener);        model.addChangeListener(changeListener);        model.addItemListener(itemListener);      }    firePropertyChange(MODEL_CHANGED_PROPERTY, old, model);    revalidate();    repaint();  } protected void init(String text, Icon icon)  {    this.text = text;    default_icon = icon;    model = new DefaultButtonModel();    actionListener = createActionListener();    changeListener = createChangeListener();    itemListener = createItemListener();    model.addActionListener(actionListener);    model.addChangeListener(changeListener);    model.addItemListener(itemListener);    horizontalAlignment = CENTER;    horizontalTextPosition = TRAILING;    verticalAlignment = CENTER;    verticalTextPosition = CENTER;    borderPainted = true;    contentAreaFilled = true;    focusPainted = true;    setFocusable(true);    setAlignmentX(LEFT_ALIGNMENT);    setAlignmentY(CENTER_ALIGNMENT);    setDisplayedMnemonicIndex(-1);     }   /**   * Get the action command string for this button's model.   *   * @return The current action command string from the button's model   */  public String getActionCommand()  {    return getModel().getActionCommand();  }  /**   * Set the action command string for this button's model.   *   * @param aCommand The new action command string to set in the button's   * model.   */  public void setActionCommand(String aCommand)  {    getModel().setActionCommand(aCommand);  }  /**   * Adds an ActionListener to the button's listener list. When the   * button's model is clicked it fires an ActionEvent, and these   * listeners will be called.   *   * @param l The new listener to add   */  public void addActionListener(ActionListener l)  {    listenerList.add(ActionListener.class, l);  }  /**   * Removes an ActionListener from the button's listener list.   *   * @param l The listener to remove   */  public void removeActionListener(ActionListener l)  {    listenerList.remove(ActionListener.class, l);  }  /**   * Returns all added <code>ActionListener</code> objects.   *    * @return an array of listeners   *    * @since 1.4   */  public ActionListener[] getActionListeners()  {    return (ActionListener[]) listenerList.getListeners(ActionListener.class);  }  /**   * Adds an ItemListener to the button's listener list. When the button's   * model changes state (between any of ARMED, ENABLED, PRESSED, ROLLOVER   * or SELECTED) it fires an ItemEvent, and these listeners will be   * called.   *   * @param l The new listener to add   */  public void addItemListener(ItemListener l)  {    listenerList.add(ItemListener.class, l);  }  /**   * Removes an ItemListener from the button's listener list.   *   * @param l The listener to remove   */  public void removeItemListener(ItemListener l)  {    listenerList.remove(ItemListener.class, l);  }  /**   * Returns all added <code>ItemListener</code> objects.   *    * @return an array of listeners   *    * @since 1.4   */  public ItemListener[] getItemListeners()  {    return (ItemListener[]) listenerList.getListeners(ItemListener.class);  }  /**   * Adds a ChangeListener to the button's listener list. When the button's   * model changes any of its (non-bound) properties, these listeners will be   * called.    *   * @param l The new listener to add   */  public void addChangeListener(ChangeListener l)  {    listenerList.add(ChangeListener.class, l);  }  /**   * Removes a ChangeListener from the button's listener list.   *   * @param l The listener to remove   */  public void removeChangeListener(ChangeListener l)  {    listenerList.remove(ChangeListener.class, l);  }  /**   * Returns all added <code>ChangeListener</code> objects.   *    * @return an array of listeners   *    * @since 1.4   */  public ChangeListener[] getChangeListeners()  {    return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);  }  /**   * Calls {@link ItemListener.itemStateChanged} on each ItemListener in   * the button's listener list.   *   * @param e The event signifying that the button's model changed state   */  protected void fireItemStateChanged(ItemEvent e)  {    e.setSource(this);    ItemListener[] listeners = getItemListeners();     for (int i = 0; i < listeners.length; i++)      listeners[i].itemStateChanged(e);  }  /**   * Calls {@link ActionListener.actionPerformed} on each {@link   * ActionListener} in the button's listener list.   *   * @param e The event signifying that the button's model was clicked   */  protected void fireActionPerformed(ActionEvent e)  {    e.setSource(this);    ActionListener[] listeners = getActionListeners();        for (int i = 0; i < listeners.length; i++)      listeners[i].actionPerformed(e);  }  /**   * Calls {@link ChangeEvent.stateChanged} on each {@link ChangeListener}   * in the button's listener list.   */  protected void fireStateChanged()  {    ChangeListener[] listeners = getChangeListeners();    for (int i = 0; i < listeners.length; i++)      listeners[i].stateChanged(changeEvent);  }  /**   * Get 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.   *   * @return The button's current keyboard mnemonic   */  public int getMnemonic()  {    return getModel().getMnemonic();  }  /**   * 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(char mne)  {    setMnemonic((int) mne);  }  /**   * 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)  {    int old = getModel().getMnemonic();    if (old != mne)      {	getModel().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)  {    getModel().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()  {    return getModel().isSelected();  }  /**   * 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)  {    super.setEnabled(b);    getModel().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>,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?