日期:2014-05-20  浏览次数:20789 次

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);
    }
}