JComboBox和ItemListener / ActionListener

时间:2015-09-24 01:42:55

标签: java swing actionlistener itemlistener

我正在为你有JComboBox的类创建一个程序,当选择一个选项时,它会弹出一个带有不同选项的窗口。我有一个选项,弹出一个新窗口,上面有两个按钮。

首先,我不确定是否应该为JComboBox选项使用ItemListener或ActionListener。现在我有一个ItemListener,我认为它只适用于“矩阵”选项,但它适用于两个选项,我无法弄清楚原因。我会发布所有代码以防万一,但我会在指定问题的上方和下方添加星标。

感谢您的帮助或指导我朝着正确的方向前进!

public class MultiForm extends JFrame{


    private JComboBox menu;
    private JButton bluePill;
    private JButton redPill;
    private JLabel matrix;
    private int matrixSelection;
    private static String[] fileName = {"", "The Matrix", "Another Option"};

public MultiForm() {
    super("Multi Form Program");        
    setLayout(new FlowLayout());
    menu = new JComboBox(fileName);
    add(menu);

 *************************************************************************  
    TheHandler handler = new TheHandler();
    menu.addItemListener(handler);      
}

private class TheHandler implements ItemListener{
    public void itemStateChanged(ItemEvent event) {

        if(event.getStateChange() == ItemEvent.SELECTED) {
            menu.setSelectedItem("The Matrix");
            menu.getSelectedIndex();
*************************************************************************
            //Create a new window when "The Matrix" is clicked in the JCB
            JFrame newFrame = new JFrame();
            JPanel panel = new JPanel();

            newFrame.setLayout(new FlowLayout());
            newFrame.setSize(500, 300);
            newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);
            add(panel, BorderLayout.CENTER);

            matrix = new JLabel("<html>After this, there is no turning back. "
                    + "<br>You take the blue pill—the story ends, you wake up "
                    + "<br>in your bed and believe whatever you want to believe."
                    + "<br>You take the red pill—you stay in Wonderland, and I show"
                    + "<br>you how deep the rabbit hole goes. Remember: all I'm "
                    + "<br>offering is the truth. Nothing more.</html>");
            newFrame.add(matrix, BorderLayout.NORTH);

            Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
            bluePill = new JButton("Blue Pill", bp);
            newFrame.add(panel.add(bluePill));  

            Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
            redPill = new JButton("Red Pill", rp);
            newFrame.add(panel.add(redPill));   


            newFrame.setVisible(true);
        }   
    }
}

public static void main(String[] args) {
    MultiForm go = new MultiForm();
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    go.setSize(400, 200);
    go.setVisible(true);
}
}

1 个答案:

答案 0 :(得分:2)

ItemListenerActionListener将告知有关组合框的更改内容。然后,您需要确定已更改的内容并采取适当的措施

例如......

private class TheHandler implements ItemListener{
    public void itemStateChanged(ItemEvent event) {

        if(event.getStateChange() == ItemEvent.SELECTED) {
            Object source = event.getSource();
            if (source instanceof JComboBox) {
                JComboBox cb = (JComboBox)source;
                Object selectedItem = cb.getSelectedItem();
                if ("The Matrix".equals(selectedItem)) {
                    // Do the matrix
                } else if ("Another Option".equals(selectedItem)) {
                    // Do another option
                }
            }
        }   
    }
}

这只是检查selectedItem是什么,并根据选择的内容采取适当的行动。您也可以使用selectedIndex来代替所选项目int,但这对您来说更容易。

有关详细信息,请查看How to Use Combo Boxes

如果您只想知道选择某个项目,您可能会发现ActionListener更简单,因为您不需要检查状态(SELECTED / {{1} }),因为它只触发选定的状态更改