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

📄 itemeventdemo.java

📁 本java源程序包括了大量的学习程序(共27章)方便大家学习
💻 JAVA
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ItemEventDemo extends JFrame 
{
	private JPanel p1,p2,p3,p4;
    private static JLabel label;
    
	private JRadioButton radioButton1, radioButton2, radioButton3;
	private ButtonGroup buttonGroup;
	
    private JComboBox comboBox;
    private String names[] = {"第一项", "第二项", "第三项"};
    
    private JCheckBox checkBox1, checkBox2;
    
    public ItemEventDemo()
    {
		super("选项事件");
		setSize(400, 400);  
		try
		{ //设置外观
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}catch(Exception e){}	
		//创建面板对象
		p1 = new JPanel();
		p2 = new JPanel();
		p3 = new JPanel();
		p4 = new JPanel();
		
		//创建单选按钮组件对象
		buttonGroup = new ButtonGroup();
		radioButton1 = new JRadioButton("radioButton1", true);
		radioButton2 = new JRadioButton("radioButton2");
		radioButton3 = new JRadioButton("radioButton3");
		//将三个JRadioButton对象设置为一个组
		buttonGroup.add(radioButton1);
		buttonGroup.add(radioButton2);
		buttonGroup.add(radioButton3);
		//将三个JRadioButton组建对象加入p1面板对象中
		p1.add(radioButton1);
		p1.add(radioButton2);
		p1.add(radioButton3);
		//为三个JRadioButton组建对象添加事件监听器
		JRadioButtonEvent jRadioButtonEvent = new JRadioButtonEvent();
		radioButton1.addItemListener(jRadioButtonEvent);
		radioButton2.addItemListener(jRadioButtonEvent);
		radioButton3.addItemListener(jRadioButtonEvent);
		
		//创建复选框组件对象
		checkBox1 = new JCheckBox("checkBox1");
		checkBox2 = new JCheckBox("checkBox2");
		//将两个JCheckBox组件对象加入p2面板对象中
		p2.add(checkBox1);
		p2.add(checkBox2);
		//为两个个JRadioButton组建对象添加事件监听器
		JCheckBoxEvent jCheckBoxEvent = new JCheckBoxEvent();
		checkBox1.addItemListener(jCheckBoxEvent);
		checkBox2.addItemListener(jCheckBoxEvent);
		
		//创建下拉菜单组件对象
		comboBox = new JComboBox(names);
		//设置用户单击下拉列表时所能显示的列表项的最大数目
		comboBox.setMaximumRowCount(3);
		//设置默认的选择项
		comboBox.setSelectedIndex(0);
		//设置字体
		comboBox.setFont(new Font("Serif", Font.PLAIN, 14));
		//注册监听器
		JComboBoxEvent jComboBoxEvent = new JComboBoxEvent();
		comboBox.addItemListener(jComboBoxEvent);
		//将下拉列表组件对象添加到p3面板中
		p3.add(comboBox);
		
	    //创建标签组件对象
	    label = new JLabel("组件事件:");
	    //将标签组件对象添加到p4面板中
	    p4.setLayout(null);
	    label.setBounds(50,10,300,30);
	    label.setFont(new Font("Serif", Font.PLAIN, 14));
	    p4.add(label);
		
        //获取内容面板
		Container container = getContentPane();
		//设置内容面板的布局管理器
		container.setLayout(new GridLayout(4, 1));
	    //将p1、p2、p3和p4四个面板加入到内容面板中
	    container.add(p1);
	    container.add(p2);
	    container.add(p3);
	    container.add(p4);
	    setVisible(true);
    }
    
	public static void main(String[] args) 
	{
		ItemEventDemo demo = new ItemEventDemo();
		demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

  class JRadioButtonEvent implements ItemListener 
  { //自定义内部类实现ItemListener接口,用于处理JRadioButton组件事件
	public void itemStateChanged(ItemEvent event)
	{
	  if(radioButton1.isSelected())
			label.setText("组件事件:radioButton1被选中");
	  else if(radioButton2.isSelected())
			label.setText("组件事件:radioButton2被选中");
	  else if(radioButton3.isSelected())
			label.setText("组件事件:radioButton3被选中");
	}
  }
  
  class JCheckBoxEvent implements ItemListener 
  { //自定义内部类实现ItemListener接口,用于处理JCheckBox组件事件
	public void itemStateChanged(ItemEvent event)
	{		
	  if(event.getItem() == checkBox1 && event.getStateChange()==ItemEvent.DESELECTED)
		label.setText("组件事件:checkBox1选项被取消");
      else if(event.getItem() == checkBox1&&event.getStateChange()==ItemEvent.SELECTED)
		label.setText("组件事件:checkBox1被选中");
      else if(event.getItem() == checkBox2 && event.getStateChange()==ItemEvent.DESELECTED)
		label.setText("组件事件:checkBox2选项被取消");
	  else if(event.getItem() == checkBox2&&event.getStateChange()==ItemEvent.SELECTED)
		label.setText("组件事件:checkBox2被选中");
	}
  }
  
  class JComboBoxEvent implements ItemListener 
  { //自定义内部类实现ItemListener接口,用于处理JComboBox组件事件
	public void itemStateChanged(ItemEvent event)
	{		
	  if(event.getStateChange() == event.SELECTED)
	  {
	     label.setText("组件事件:"+names[comboBox.getSelectedIndex()]+"被选中");
	  }
	}
  }  
}

⌨️ 快捷键说明

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