⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propchangeeventdemo.java

📁 《java事件处理指南》一书的代码,好东西
💻 JAVA
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;public class PropChangeEventDemo extends JFrame implements ItemListener{   NewLabel label1, label2, label3;    FontColor fontColor;   JComboBox jcb;   public PropChangeEventDemo()   {/*  A FontColor object is created.  This object maintains a Color   *//*  object as a bound property.                                     */      fontColor = new FontColor(Color.black);/*  Three NewLabel objects are created and placed on a JFrame.  The  *//*  NewLabel class is a JLabel that uses a FontColor object to set   *//*  the color of the label text.                                     */      label1 = new NewLabel("hello", fontColor);      label1.setFont(new Font("Serif", Font.BOLD, 14));      label2 = new NewLabel("there", fontColor);      label2.setFont(new Font("Serif", Font.BOLD, 14));      label3 = new NewLabel("boys", fontColor);      label3.setFont(new Font("Serif", Font.BOLD, 14));/*  A JComboBox is used to change the color of the NewLabel object text  *//*  The JComboBox registers an ItemListener.                             */      String[] colorList = {"black", "red", "blue", "green"};      jcb = new JComboBox(colorList);      jcb.setSelectedIndex(0);      jcb.addItemListener(this);      JPanel centerPanel = new JPanel();      centerPanel.add(label1);      centerPanel.add(label2);      centerPanel.add(label3);      JPanel southPanel = new JPanel();      southPanel.add(jcb);      getContentPane().add(centerPanel, BorderLayout.CENTER);      getContentPane().add(southPanel, BorderLayout.WEST);      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      setBounds(100, 100, 300, 200);      setVisible(true);   }/*  Since the PropChangeLstnrDemo class serves as the ItemListener,   *//*  it provides an implementation of the itemStateChanged() method.   *//*  The FontColor object calls its setColor() method passing it the   *//*  selected color.                                                   */   public void itemStateChanged(ItemEvent event)   {      JComboBox comboBox = (JComboBox)event.getItemSelectable();      String color = (String)comboBox.getSelectedItem();      if ( color.equals("black") )              fontColor.setColor(Color.black);       if ( color.equals("red") )              fontColor.setColor(Color.red);       if ( color.equals("blue") )              fontColor.setColor(Color.blue);       if ( color.equals("green") )              fontColor.setColor(Color.green);    }   public static void main(String args[])   {      PropChangeEventDemo demo = new PropChangeEventDemo();   }}

⌨️ 快捷键说明

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