Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.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 将多个actionListener添加到多个JButton_Java_Swing_Actionlistener - Fatal编程技术网

Java 将多个actionListener添加到多个JButton

Java 将多个actionListener添加到多个JButton,java,swing,actionlistener,Java,Swing,Actionlistener,我正在使用GUI,并尝试使用不同的按钮来执行不同的任务 目前,每个按钮都指向同一个ActionListener public class GUIController { public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI();

我正在使用GUI,并尝试使用不同的按钮来执行不同的任务

目前,每个按钮都指向同一个ActionListener

public class GUIController {

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
public static void createAndShowGUI() {
    JFrame frame = new JFrame("GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new GridLayout(3,3));

    JLabel leight =new JLabel("8");
    frame.getContentPane().add(leight);
    JLabel lfive =new JLabel("0");
    frame.getContentPane().add(lfive);
    JLabel lthree =new JLabel("0");
    frame.getContentPane().add(lthree);

    JButton beight =new JButton("Jug 8");
    frame.getContentPane().add(beight);
    JButton bfive =new JButton("Jug 5");
    frame.getContentPane().add(bfive);
    JButton bthree =new JButton("Jug 3");
    frame.getContentPane().add(bthree);

    LISTN ccal = new LISTN (leight,lfive,lthree);

    beight.addActionListener(ccal);
    bfive.addActionListener(ccal);
    bthree.addActionListener(ccal);

    frame.pack();
    frame.setVisible(true);

}

}
我的actionlistener文件

public class JugPuzzleGUILISTN implements ActionListener {
JLabel leight;
JLabel lfive;
JLabel lthree;

JugPuzzleGUILISTN(JLabel leight,JLabel lfive, JLabel lthree){
    this.leight = leight;
    this.lfive = lfive;
    this.lthree = lthree;
}

public void actionPerformed(ActionEvent e) {
    }

}
}
我在ActionEvent中编写的任何内容都适用于所有三个按钮,如何使每个按钮都有自己的功能? 非常感谢你

我在ActionEvent中编写的任何内容都适用于所有三个按钮,如何使每个按钮都有自己的功能

您希望所有3个按钮都能够触发类似的操作。但是,您还需要为每个按钮实现不同的功能

其中一种方法是再创建3个侦听器,每个侦听器将添加到各自的按钮中。因此,现在每个按钮将添加2个侦听器(您当前的侦听器+新创建的侦听器)

还有其他方法,比如在当前侦听器中使用if语句来检查单击了哪个按钮,但我发现单独的侦听器更容易维护,代码耦合更低

我在ActionEvent中编写的任何内容都适用于所有三个按钮,如何使每个按钮都有自己的功能

您希望所有3个按钮都能够触发类似的操作。但是,您还需要为每个按钮实现不同的功能

其中一种方法是再创建3个侦听器,每个侦听器将添加到各自的按钮中。因此,现在每个按钮将添加2个侦听器(您当前的侦听器+新创建的侦听器)

还有其他方法,比如在当前侦听器中使用if语句来检查单击了哪个按钮,但我发现单独的侦听器更容易维护,代码耦合更低

如何使每个按钮都有自己的功能

向每个按钮添加不同的ActionListener

更好的方法是使用
操作
而不是ActionListener。
Action
只是一个具有更多属性的奇特的ActionListener

阅读Swing教程中关于定义内部类的示例部分,以便为每个按钮创建唯一的
操作

如何使每个按钮都有自己的功能

向每个按钮添加不同的ActionListener

更好的方法是使用
操作
而不是ActionListener。
Action
只是一个具有更多属性的奇特的ActionListener


阅读Swing教程中关于定义内部类的部分,以便为每个按钮创建唯一的
操作。

如何使每个按钮都有自己的功能?
-为每个按钮添加不同的ActionListener。您可以使用单个侦听器并使用
actionCommand
source
属性;您可以使用
clientProperty
;您可以为每个按钮使用单独的侦听器;您可以使用
操作
API。。。许多有很好文档记录的想法
如何使每个按钮都有自己的功能?
-为每个按钮添加不同的ActionListener。您可以使用单个侦听器并使用
actionCommand
source
属性;您可以使用
clientProperty
;您可以为每个按钮使用单独的侦听器;您可以使用
操作
API。。。有很多记录在案的想法
//Example:

beight.addActionListener(ccal);
bfive.addActionListener(ccal);
bthree.addActionListener(ccal);

beight.addActionListener(ccal_beight);
bfive.addActionListener(ccal_bfive);
bthree.addActionListener(ccal_bthree);