简体   繁体   中英

Adding ActionListeners and calling methods in other classes

I need some help, as I am quite the noob.

The program im trying to make here, used to work for my intentions, but as I tried to make my code more readable, I ran into a problem regarding ActionListener .

Before I made a new class to have all the methods in, I used button.addActionListener(this); and it worked just fine. Now that I wanted to put things in a separate class, I have absolutely no idea what to do.

So I guess my question is, how can I make ActionListener work in a situation like this, or am I just doing everything wrong here?

Here's the part of my code that I think is relevant(edited out most of it):

    //Class with frame, panels, labels, buttons, etc.

    class FemTreEnPlus {
        FemTreEnPlus() {
            //Components here!

            //Then to the part where I try to add these listeners
            cfg.addActionListener();
            Exit.addActionListener();
            New.addActionListener();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
        public void run(){
        //Start the Program in the FemTreEnPlus Class
            new FemTreEnPlus();
        }     
    });  
}

That was the class with the frame, here's the other class, with the methods

public class FemTreEnMethods extends FemTreEnPlus implements ActionListener {

       //Perform Actions!
   public void actionPerformed(ActionEvent ae){  
       if(ae.getSource() == cfgButton){
        configureSettings();
       }
       if(ae.getSource() == newButton){
        newProject();
       }     
       if(ae.getSource() == exitButton){
        exitProgram();
       }
 }

   //All methods are down here

Thanks in advance for any help.

Despite the tutorials' examples show the use of listeners implemented in the way you do, IMHO is more useful use anonymous inner classes to implement listeners. For instance:

cfgButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerfomed(ActionEvent e) {
        // do the stuff related to cfgButton here
    }
};

newButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerfomed(ActionEvent e) {
        // do the stuff related to newButton here
    }
};

exitButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerfomed(ActionEvent e) {
        // do the stuff related to exitButton here
    }
};

This approach has these advantages:

  • Listeners logic is well separated.
  • You don't need those nested if blocks asking who is the source of the event.
  • If you add a new button you don't have to modify your listener. Just add a new one.

Of course it depends on the case. If the behaviour will be the same for a set of components (for instance radio buttons or check boxes) then it makes sense have only one listener and use EventObject.getSource() to work with the event's source. This approach is suggested here and exemplified here . Note the examples also make use of anonymous inner classes in this way:

ActionListener actionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // do something here
    }
};

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