JComboBox 和 ItemListener / ActionListener

JComboBox and ItemListener / ActionListener

I am creating a program for class where you have a JComboBox and when one option is selected it pops up a window with different options.我有一个选项可以弹出一个新的 window,上面有两个按钮。

首先,我不确定是否应该为 JComboBox 选项使用 ItemListener 或 ActionListener。现在我有一个 ItemListener,我认为它只适用于 "The Matrix" 选项,但它适用于两个选项,我不知道为什么。我会 post 我所有的代码以防万一,但我会在指定问题的上方和下方添加星号。

感谢您的帮助或为我指明正确的方向!

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

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,这将 return 一个 int 表示所选项目,但哪个对您来说更容易。

查看 How to Use Combo Boxes 了解更多详情

如果您只想知道项目何时被选中,您可能会发现 ActionListener 更简单,因为您不需要检查状态 (SELECTED/UNSELECTED), 因为它只在选定的状态改变时触发