abstractbutton.java

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

JAVA
1,946
字号
   * 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   * resulting ChangeListener in its <code>changeListener</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 The new ChangeListener   */  protected  ChangeListener createChangeListener()  {    return new ChangeListener()      {        public void stateChanged(ChangeEvent e)        {          AbstractButton.this.fireStateChanged();          AbstractButton.this.repaint();                  }      };  }  /**   * <p>Factory method which creates a {@link ItemListener}, used to   * subscribe to ItemEvents from the button's model. Subclasses of   * AbstractButton may wish to override the listener used to subscribe to   * such ItemEvents. By default, the listener just propagates the   * {@link ItemEvent} to the button's ItemListeners, via the {@link   * AbstractButton.fireItemStateChanged} method.</p>   *   * <p>The button calls this method during construction, stores the   * resulting ItemListener in its <code>changeListener</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>   *   * <p>Note that ItemEvents are only generated from the button's model   * when the model's <em>selected</em> property changes. If you want to   * subscribe to other properties of the model, you must subscribe to   * ChangeEvents.   *   * @return The new ItemListener   */  protected  ItemListener createItemListener()  {    return new ItemListener()      {        public void itemStateChanged(ItemEvent e)        {          AbstractButton.this.fireItemStateChanged(e);        }      };  }  /**   * Programmatically perform a "click" on the button: arming, pressing,   * waiting, un-pressing, and disarming the model.   */  public void doClick()  {    doClick(100);  }  /**   * Programmatically perform a "click" on the button: arming, pressing,   * waiting, un-pressing, and disarming the model.   *   * @param pressTime The number of milliseconds to wait in the pressed state   */  public void doClick(int pressTime)  {    getModel().setArmed(true);    getModel().setPressed(true);    try      {        java.lang.Thread.sleep(pressTime);      }    catch (java.lang.InterruptedException e)      {        // probably harmless      }    getModel().setPressed(false);    getModel().setArmed(false);  }  /**   * Return the button's disabled selected icon. The look and feel class   * should paint this icon when the "enabled" property of the button's model   * is <code>false</code> and its "selected" property is   * <code>true</code>. This icon can be <code>null</code>, in which case   * it is synthesized from the button's selected icon.   *   * @return The current disabled selected icon   */  public Icon getDisabledSelectedIcon()  {    return disabledSelectedIcon;  }  /**   * Set the button's disabled selected icon. The look and feel class   * should paint this icon when the "enabled" property of the button's model   * is <code>false</code> and its "selected" property is   * <code>true</code>. This icon can be <code>null</code>, in which case   * it is synthesized from the button's selected icon.   *   * @param icon The new disabled selected icon   */  public void setDisabledSelectedIcon(Icon icon)  {    if (disabledSelectedIcon == icon)      return;        Icon old = disabledSelectedIcon;    disabledSelectedIcon = icon;    firePropertyChange(DISABLED_SELECTED_ICON_CHANGED_PROPERTY, old, icon);    revalidate();    repaint();          }  /**   * Return the button's rollover icon. The look and feel class should   * paint this icon when the "rolloverEnabled" property of the button is   * <code>true</code> and the mouse rolls over the button.   *   * @return The current rollover icon   */  public Icon getRolloverIcon()  {    return rolloverIcon;  }  /**   * Set the button's rollover icon. The look and feel class should   * paint this icon when the "rolloverEnabled" property of the button is   * <code>true</code> and the mouse rolls over the button.   *   * @param rolloverIcon The new rollover icon   */  public void setRolloverIcon(Icon r)  {    if (rolloverIcon == r)      return;        Icon old = rolloverIcon;    rolloverIcon = r;    firePropertyChange(ROLLOVER_ICON_CHANGED_PROPERTY, old, rolloverIcon);    revalidate();    repaint();  }  /**   * Return the button's rollover selected icon. The look and feel class   * should paint this icon when the "rolloverEnabled" property of the button   * is <code>true</code>, the "selected" property of the button's model is   * <code>true</code>, and the mouse rolls over the button.   *   * @return The current rollover selected icon   */  public Icon getRolloverSelectedIcon()  {    return rolloverSelectedIcon;  }  /**   * Set the button's rollover selected icon. The look and feel class   * should paint this icon when the "rolloverEnabled" property of the button   * is <code>true</code>, the "selected" property of the button's model is   * <code>true</code>, and the mouse rolls over the button.   *   * @param rolloverSelectedIcon The new rollover selected icon   */  public void setRolloverSelectedIcon(Icon r)  {    if (rolloverSelectedIcon == r)      return;        Icon old = rolloverSelectedIcon;    rolloverSelectedIcon = r;    firePropertyChange(ROLLOVER_SELECTED_ICON_CHANGED_PROPERTY, old, r);    revalidate();    repaint();  }  /**   * Return the button's selected icon. The look and feel class should   * paint this icon when the "selected" property of the button's model is   * <code>true</code>, and either the "rolloverEnabled" property of the   * button is <code>false</code> or the mouse is not currently rolled   * over the button.   *   * @return The current selected icon   */  public Icon getSelectedIcon()  {    return selectedIcon;  }  /**   * Set the button's selected icon. The look and feel class should   * paint this icon when the "selected" property of the button's model is   * <code>true</code>, and either the "rolloverEnabled" property of the   * button is <code>false</code> or the mouse is not currently rolled   * over the button.   *   * @param selectedIcon The new selected icon   */  public void setSelectedIcon(Icon s)  {    if (selectedIcon == s)      return;        Icon old = selectedIcon;    selectedIcon = s;    firePropertyChange(SELECTED_ICON_CHANGED_PROPERTY, old, s);    revalidate();    repaint();  }  /**   * Returns an single-element array containing the "text" property of the   * button if the "selected" property of the button's model is   * <code>true</code>, otherwise returns <code>null</code>.   *   * @return The button's "selected object" array   */  public Object[] getSelectedObjects()  {    if (isSelected())      {        Object[] objs = new Object[1];        objs[0] = getText();        return objs;      }    else     {        return null;      }  }  /**   * Called when image data becomes available for one of the button's icons.   *   * @param img The image being updated   * @param infoflags One of the constant codes in {@link ImageObserver} used to describe   * updated portions of an image.   * @param x X coordinate of the region being updated   * @param y Y coordinate of the region being updated   * @param w Width of the region beign updated   * @param h Height of the region being updated   *   * @return <code>true</code> if img is equal to the button's current   * icon, otherwise <code>false</code>   */  public boolean imageUpdate(Image img, int infoflags, int x, int y, int w,                             int h)  {    return current_icon == img;  }  /**   * Returns the value of the button's "contentAreaFilled" property. This   * property indicates whether the area surrounding the text and icon of   * the button should be filled by the look and feel class.  If this   * property is <code>false</code>, the look and feel class should leave   * the content area transparent.   *   * @return The current value of the "contentAreaFilled" property   */  public boolean isContentAreaFilled()  {    return contentAreaFilled;  }  /**   * Sets the value of the button's "contentAreaFilled" property. This   * property indicates whether the area surrounding the text and icon of   * the button should be filled by the look and feel class.  If this   * property is <code>false</code>, the look and feel class should leave   * the content area transparent.   *   * @param b The new value of the "contentAreaFilled" property   */  public void setContentAreaFilled(boolean b)  {    if (contentAreaFilled == b)      return;        boolean old = contentAreaFilled;    contentAreaFilled = b;    firePropertyChange(CONTENT_AREA_FILLED_CHANGED_PROPERTY, old, b);    revalidate();    repaint();   }  /**   * Paints the button's border, if the button's "borderPainted" property is   * <code>true</code>, by out calling to the button's look and feel class.   *   * @param g The graphics context used to paint the border   */  protected void paintBorder(Graphics g)  {    if (isBorderPainted())      super.paintBorder(g);  }  /**   * Returns a string, used only for debugging, which identifies or somehow   * represents this button. The exact value is implementation-defined.   *   * @return A string representation of the button   */  protected String paramString()  {    StringBuffer sb = new StringBuffer();    sb.append(super.paramString());    sb.append(",defaultIcon=");    if (getIcon() != null)      sb.append(getIcon());    sb.append(",disabledIcon=");    if (getDisabledIcon() != null)      sb.append(getDisabledIcon());    sb.append(",disabledSelectedIcon=");    if (getDisabledSelectedIcon() != null)      sb.append(getDisabledSelectedIcon());    sb.append(",margin=");    if (getMargin() != null)      sb.append(getMargin());    sb.append(",paintBorder=").append(isBorderPainted());    sb.append(",paintFocus=").append(isFocusPainted());    sb.append(",pressedIcon=");    if (getPressedIcon() != null)      sb.append(getPressedIcon());    sb.append(",rolloverEnabled=").append(isRolloverEnabled());    sb.append(",rolloverIcon=");    if (getRolloverIcon() != null)      sb.append(getRolloverIcon());    sb.append(",rolloverSelected=");    if (getRolloverSelectedIcon() != null)      sb.append(getRolloverSelectedIcon());    sb.append(",selectedIcon=");    if (getSelectedIcon() != null)      sb.append(getSelectedIcon());    sb.append(",text=");    if (getText() != null)      sb.append(getText());    return sb.toString();  }  /**   * Set the "UI" property of the button, which is a look and feel class   * responsible for handling the button's input events and painting it.   *   * @param ui The new "UI" property   */  public void setUI(ButtonUI ui)  {    super.setUI(ui);  }    /**   * Set the "UI" property of the button, which is a look and feel class   * responsible for handling the button's input events and painting it.   *   * @return The current "UI" property   */  public ButtonUI getUI()  {    return (ButtonUI) ui;  }    /**   * Set the "UI" property to a class constructed, via the {@link   * UIManager}, from the current look and feel. This should be overridden   * for each subclass of AbstractButton, to retrieve a suitable {@link   * ButtonUI} look and feel class.   */  public void updateUI()  {  }  /**   * Returns the current time in milliseconds in which clicks gets coalesced   * into a single <code>ActionEvent</code>.   *   * @return the time in milliseconds   *    * @since 1.4   */  public long getMultiClickThreshhold()  {    return multiClickThreshhold;  }  /**   * Sets the time in milliseconds in which clicks gets coalesced into a single   * <code>ActionEvent</code>.   *   * @param threshhold the time in milliseconds   *    * @since 1.4   */  public void setMultiClickThreshhold(long threshhold)  {    if (threshhold < 0)      throw new IllegalArgumentException();    multiClickThreshhold = threshhold;  }}

⌨️ 快捷键说明

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