简体   繁体   中英

Add multiple actionListener to multiple JButtons

I am working on a GUI and trying to get different buttons to perform different tasks.

Currently, each button is leading to the same 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);

}

}

my actionlistener file

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

}
}

Anything I write in ActionEvent applies to all three buttons, how can I make it so that each button has their own function? Thank you so much!

how can I make it so that each button has their own function

Add a different ActionListener to each button.

Better yet, use an Action instead of an ActionListener. An Action is just a fancy ActionListener that has a few more properties.

Read the section from the Swing tutorial on How to Use Action for examples of defining inner classes so you can create a unique Action for each button.

Anything I write in ActionEvent applies to all three buttons, how can I make it so that each button has their own function?

You have similar actions which you want all the 3 buttons to be able to trigger. However you also have different functions which you want to implement for each button.

One of the ways will creating 3 more listeners, each to be added to their respective button. So each button now will be added with 2 listeners (your current one + newly created ones).

//Example:

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

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

There are other ways such as using if-statements in your current listener to check which button is clicked, but I find separate listeners easier to maintain with lower code coupling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM