简体   繁体   English

在其他类中添加ActionListener和调用方法

[英]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 . 我试图在这里制作的程序曾经按照我的意图工作,但是当我试图使代码更具可读性时,我遇到了有关ActionListener的问题。

Before I made a new class to have all the methods in, I used button.addActionListener(this); 在制作新类以包含所有方法之前,我使用了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? 所以我想我的问题是,如何使ActionListener在这种情况下工作,或者我只是在这里做错了什么?

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. 尽管本教程的示例显示了以您的方式实现的侦听器的使用,但是IMHO使用匿名内部类来实现侦听器更为有用。 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块来询问谁是事件的来源。
  • 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. 如果一组组件的行为相同(例如单选按钮或复选框),则只有一个侦听器并使用EventObject.getSource()处理事件的源是有意义的。 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
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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