首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JComboBox和ItemListener / ActionListener

JComboBox和ItemListener / ActionListener
EN

Stack Overflow用户
提问于 2015-09-24 01:42:54
回答 1查看 8.1K关注 0票数 2

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

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

谢谢你的帮助或指点我的正确方向!

代码语言:javascript
复制
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);
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-24 01:58:51

当组合框的某些内容发生变化时,ItemListenerActionListener会告诉您。然后,你需要确定什么已经改变,并采取适当的行动。

例如..。

代码语言:javascript
复制
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,但这对您来说更容易。

有关更多细节,请查看如何使用组合框

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32752072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档