Java Swing JCheckbox JCombBox的组件

package com.test;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import com.cody.JCheckCombo;

public class TestFrame
{
    /**
     * @author cody 2011-2-27
     * @param args
     */
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.add(new TestFrame().getComponent());
        frame.setSize(500, 80);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    public JComponent getComponent()
    {
        final JCheckCombo combo = new JCheckCombo(" + ", "请选择选项");
        combo.setPreferredSize(new Dimension(200, 22));
        combo.addItem(new TestStructData("第一个复选框"));
        combo.addItem(new TestStructData("第二个复选框"));
        combo.addItem(new TestStructData("第三个复选框"));
        combo.addItem(new TestStructData("第四个复选框"));
        TestStructData data = new TestStructData("第一个特殊选项");
        data.setNeedCheckbox(false);
        combo.addItem(data);
        combo.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                showAlert(combo);
            }
        });
        combo.setStartListen(true);
        return combo;
    }
    
    private void showAlert(JCheckCombo combo)
    {
        Object obj = combo.getSelectedItem();
        if (obj instanceof TestStructData)
        {
            if (!((TestStructData) obj).isNeedCheckbox())
            {
                JOptionPane.showMessageDialog(null, ((TestStructData) obj).toString());
            }
        }
    }
}

你可能感兴趣的:(java,swing)