Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JButton上的ActionListener_Java_Swing_User Interface_Button_Actionlistener - Fatal编程技术网

Java JButton上的ActionListener

Java JButton上的ActionListener,java,swing,user-interface,button,actionlistener,Java,Swing,User Interface,Button,Actionlistener,可以在不同的按钮上添加不同的ActionListener吗? 我的问题是我有一个JComboBox来设置游戏的难度等级,还有一个按钮可以启动游戏 所以问题是,我怎样才能选择难度等级,然后点击另一个按钮开始游戏 您不需要“不同的ActionListener”,只需要从JButton的ActionListener中的JComboBox中获取结果,并使用该结果确定程序应该走的方向 myButton.addActionListener(e -> { // get combo selecti

可以在不同的按钮上添加不同的ActionListener吗? 我的问题是我有一个JComboBox来设置游戏的难度等级,还有一个按钮可以启动游戏

所以问题是,我怎样才能选择难度等级,然后点击另一个按钮开始游戏

您不需要“不同的ActionListener”,只需要从JButton的ActionListener中的JComboBox中获取结果,并使用该结果确定程序应该走的方向

myButton.addActionListener(e -> {
    // get combo selection -- assuming that it holds Strings. Better if it held enums though
    String selection = (String) myCombo.getSelectedItem();
    
    // here use if blocks or a switch statement decide what to do
    if (selection.equals(foo)) {
        //....
    } else if (selection.equals(bar)) {
        //...
    } else if.....
});

我甚至不会向JComboBox添加侦听器,因为只有当用户选择按钮时,操作才会启动。

但我必须知道用户在JComboBox中选择了什么。这很重要,因为我需要另一个答案class@Jens:我在上面的代码中向您演示了如何执行此操作。如果您需要进一步的帮助,您必须显示您的相关代码,并显示您的困境。我们只能根据您显示的内容提供帮助,因为我们无法看到未显示的代码。最好在你的问题中发布一个程序。有关详细信息,请阅读链接。