当前位置: 代码迷 >> J2SE >> JcomboBox下拉框事件监听如何写?麻烦举个简单例子说明
  详细解决方案

JcomboBox下拉框事件监听如何写?麻烦举个简单例子说明

热度:16   发布时间:2016-04-24 01:11:40.0
JcomboBox下拉框事件监听怎么写?麻烦举个简单例子说明?
JcomboBox下拉框事件监听怎么写?麻烦举个简单例子说明?

------解决方案--------------------
Java code
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class JComboBoxFrame extends JFrame{    private JComboBox cb = new JComboBox();        public JComboBoxFrame()    {        cb.addItem("1");        cb.addItem("2");        cb.addItem("3");                cb.addItemListener(new ItemListener()            {                public void itemStateChanged(ItemEvent event)                {                    switch (event.getStateChange())                    {                    case ItemEvent.SELECTED:                         System.out.println("选中" + event.getItem());                        break;                    case ItemEvent.DESELECTED:                        System.out.println("取消选中" + event.getItem());                        break;                    }                }            });                add(cb);    }        public static void main(String[] args)    {        JFrame frame = new JComboBoxFrame();        frame.pack();        frame.setVisible(true);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }}