JComboBox 和 ItemListener / ActionListener

Noo*_*ick 3 java swing actionlistener itemlistener

我正在为班级创建一个程序,其中您有一个 JComboBox,当选择一个选项时,它会弹出一个包含不同选项的窗口。我有一个选项可以弹出一个新窗口,上面有两个按钮。

\n\n

首先,我不确定是否应该使用 ItemListener 或 ActionListener 作为 JComboBox 选项。现在我有一个 ItemListener,我认为它仅适用于“矩阵”选项,但它适用于这两个选项,我不明白为什么。我将发布所有代码以防万一,但我将在指定问题的上方和下方添加星星。

\n\n

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

\n\n
public class MultiForm extends JFrame{\n\n\n    private JComboBox menu;\n    private JButton bluePill;\n    private JButton redPill;\n    private JLabel matrix;\n    private int matrixSelection;\n    private static String[] fileName = {"", "The Matrix", "Another Option"};\n\npublic MultiForm() {\n    super("Multi Form Program");        \n    setLayout(new FlowLayout());\n    menu = new JComboBox(fileName);\n    add(menu);\n\n *************************************************************************  \n    TheHandler handler = new TheHandler();\n    menu.addItemListener(handler);      \n}\n\nprivate class TheHandler implements ItemListener{\n    public void itemStateChanged(ItemEvent event) {\n\n        if(event.getStateChange() == ItemEvent.SELECTED) {\n            menu.setSelectedItem("The Matrix");\n            menu.getSelectedIndex();\n*************************************************************************\n            //Create a new window when "The Matrix" is clicked in the JCB\n            JFrame newFrame = new JFrame();\n            JPanel panel = new JPanel();\n\n            newFrame.setLayout(new FlowLayout());\n            newFrame.setSize(500, 300);\n            newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);\n            add(panel, BorderLayout.CENTER);\n\n            matrix = new JLabel("<html>After this, there is no turning back. "\n                    + "<br>You take the blue pill\xe2\x80\x94the story ends, you wake up "\n                    + "<br>in your bed and believe whatever you want to believe."\n                    + "<br>You take the red pill\xe2\x80\x94you stay in Wonderland, and I show"\n                    + "<br>you how deep the rabbit hole goes. Remember: all I\'m "\n                    + "<br>offering is the truth. Nothing more.</html>");\n            newFrame.add(matrix, BorderLayout.NORTH);\n\n            Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));\n            bluePill = new JButton("Blue Pill", bp);\n            newFrame.add(panel.add(bluePill));  \n\n            Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));\n            redPill = new JButton("Red Pill", rp);\n            newFrame.add(panel.add(redPill));   \n\n\n            newFrame.setVisible(true);\n        }   \n    }\n}\n\npublic static void main(String[] args) {\n    MultiForm go = new MultiForm();\n    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n    go.setSize(400, 200);\n    go.setVisible(true);\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Mad*_*mer 5

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
                }
            }
        }   
    }
}
Run Code Online (Sandbox Code Playgroud)

这只是检查是什么selectedItem,并根据选择的内容采取适当的操作。您也可以使用selectedIndex相反的方法,它将返回一个int代表所选项目的值,但哪个对您来说更容易。

查看如何使用组合框了解更多详细信息

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