添加ActionListeners并在其他类中调用方法 [英] Adding ActionListeners and calling methods in other classes

查看:123
本文介绍了添加ActionListeners并在其他类中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,因为我很像菜鸟。

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

我试图在这里制作的程序,曾经用于我的意图,但是当我试图让我的代码更具可读性,我遇到了关于 ActionListener 的问题。

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.

在我创建一个新类以使用所有方法之前,我使用了 button.addActionListener(this); 它运作得很好。既然我想把东西放在一个单独的课堂上,我完全不知道该怎么做。

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.

所以我想我的问题是,如何让 ActionListener 在这样的情况下工作,或者我是只是在这里做错了什么?

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

提前感谢您的帮助。

推荐答案

尽管教程的示例显示了以您的方式实现的侦听器的使用,但恕我直言更有用的是使用匿名内部类实现监听器。例如:

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.

当然这取决于具体情况。如果一组组件(例如单选按钮或复选框)的行为相同,那么只有一个侦听器并使用 EventObject.getSource()用于处理事件的源。建议使用此方法此处并举例说明此处。注意这些示例也以这种方式使用匿名内部类:

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
    }
};

这篇关于添加ActionListeners并在其他类中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆