fontcolor2.java

来自「《java事件处理指南》一书的代码,好东西」· Java 代码 · 共 84 行

JAVA
84
字号
import javax.swing.*;import java.awt.*;import java.beans.*;public class FontColor2{   private Color color;   private PropertyChangeSupport pcs;   private VetoableChangeSupport vcs;   private Component parent;   public FontColor2(Color c, Component p)   {/*  The FontColor2 constructor initializes the Color variable as well  *//*  as a PropertyChangeSupport and VetoableChangeSupport objects.      *//*  A parent component is passed to the constructor so a JOptionPane   *//*  can be displayed if a an attempt is made to change the color to    *//*  a forbidden value.  In this example, the color green is forbidden  */       color = c;      pcs = new PropertyChangeSupport(this);      vcs = new VetoableChangeSupport(this);      parent = p;   }   public Color getColor()   {      return color;   }/*  The Color object is implemented as a constrained property. When  *//*  the setColor() method is called, the VetoableChangeSupport       *//*  object fires a PropertyChangeEvent object to any registered      *//*  VetoableChangeListeners.  These listeners determine if the       *//*  proposed change is acceptable.  If it is not, a                  *//*  PropertyVetoException is thrown.  If the change is acceptable,   *//*  the color instance variable is updated with the change and a     *//*  PropertyChangeEvent is sent to any registered                    *//*  PropertyChangeListeners.                                         */   public void setColor(Color c)   {      Color oldColor = color;      try      {         vcs.fireVetoableChange("color", oldColor, c);         color = c;      }      catch (PropertyVetoException pve)       {         JOptionPane.showMessageDialog(parent, pve.getMessage());      }      pcs.firePropertyChange("color", oldColor, color);   }/*  For the FontColor class to be able to register or de-register      *//*  a PropertyChangeListener or VetoableChangeListener, it has to      *//*  provide methods to add and remove those listeners.  The            *//*  PropertyChangeSupport object is used to manage the                 *//*  PropertyChangeListener lists and the VetoableChangeSupport object  *//*  is used to manage the VetoableChangeListener lists.                */   public void addPropertyChangeListener(PropertyChangeListener listener)   {      pcs.addPropertyChangeListener(listener);   }   public void removePropertyChangeListener(PropertyChangeListener listener)   {      pcs.removePropertyChangeListener(listener);   }   public void addVetoableChangeListener(VetoableChangeListener listener)   {      vcs.addVetoableChangeListener(listener);   }   public void removeVetoableChangeListener(VetoableChangeListener listener)   {      vcs.removeVetoableChangeListener(listener);   }}

⌨️ 快捷键说明

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