1

java中是否有可能拥有一个具有不同功能的EventHandlers的类?例如button1会让你登录,而button2会让你退出,这可能吗?这是我制作的代码,它似乎不起作用。

package event.handlers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TheHandler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent logInEvent) {
        System.out.println("Button Login");
    }

    public void actionPerformed(ActionEvent cancelEvent) {
        System.out.println("Cancel Login");
    }
}
4

5 回答 5

2

您需要两个 ActionListener 实现,每个按钮一个,或者 actionPerformed 需要通过事件参数确定按钮并采取适当的操作。您的代码将无法编译,因为这两种方法的签名相同。

于 2011-12-19T13:13:20.877 回答
1

不可以。你不能让一个类实现两个具有相同函数签名的方法。编译器如何知道为不同的事件调用哪一个?您为参数指定的名称对编译器没有任何意义。

作为替代方案,您可以创建多个匿名操作侦听器,如果您希望所有内容都在同一个类中,只需将调用转发到具有唯一名称的方法。

public class TheHandler {

    public TheHandler() {
        JButton login, cancel;

        //initialize code here

        login.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent logInEvent) {
                loginPerformed(logInEvent);
            }
        });
        cancel.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent cancelEvent) {
                cancelPerformed(cancelEvent);
            }
        });
    }

    public void loginPerformed(ActionEvent logInEvent) {
        System.out.println("Button Login");
    }

    public void cancelPerformed(ActionEvent cancelEvent) {
        System.out.println("Cancel Login");
    }
}
于 2011-12-19T13:13:16.483 回答
0

你可以使用ActionEvent 的getSource()orgetActionCommand()方法。

@Override
public void actionPerformed(ActionEvent logInEvent) {
      Object src=logInEvent.getSource();
      String cmd=logInEvent.getActionCommand(); //It will return caption of button
      if(src==btn1)
       {
           //
        } 
      //Or
      if(cmd.equals("Button1")) { ... }

}
于 2011-12-19T13:15:35.540 回答
0

一个类中不能有多个actionPerformed方法。简单的方法是根据动作来源进行操作,例如:

(在 actionPerformed 方法中)

if(e.getSource() == loginButtton) { // based on button variable if they are in same class and accessible in actionPerformed method
    loginMethod()
} else if(e.getSource == logoutButton) {
    logoutMethod()
}

或者

if(e.getActionCommand().equals("loginButtton")) { // based on caption/text on button
    loginMethod()
} else if(e.getActionCommand().equals("logoutButtton")) {
    logoutMethod()
}

或者您可以为不同的按钮设置不同的匿名类,例如

loginButton.addActionListner(new ActionListerner(){
    public void actionPerformed(ActionEvent loginEvent) {
        loginMethod();
    }
});
logoutButton.addActionListner(new ActionListerner(){
    public void actionPerformed(ActionEvent cancelEvent) {
        logoutMethod();
    }
});
于 2011-12-19T13:17:10.980 回答
0

问题在于您的两个方法签名是相同的。当 Java 试图找出调用哪个方法时,它无法区分两者之间的区别。

我可以想到两种方法来做你想做的事:

据推测,您正在注册按钮上的侦听器,例如cancelButton.addActionListener(...). 因此,您可以为每个按钮提供自己的匿名内部类:

loginButton.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent logInEvent) {
        System.out.println("Button Login");
    }
}
cancelButton.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent cancelEvent) {
        System.out.println("Cancel Login");
    }
}

或者您可以定义一个检查调用来源的 actionPerformed 方法:

public class TheHandler implements ActionListener {

    JButton loginButton;
    JButton cancelButton;

    public TheHandler()
    {
        ...
        // Now, technically, this is bad form because you're leaking 'this'.
        // But as long as this will only be called after this constructor finishes 
        // initializing, it's safe.
        loginButton.addActionListener(this);
        cancelButton.addActionListener(this);
        ...
    }

    ...

    @Override
    public void actionPerformed(ActionEvent evt) {
        if(evt.getSource() == loginButton)
            System.out.println("Button Login");
        else if(evt.getSource() == cancelButton)
            System.out.println("Cancel Login");
    }
}

使用匿名内部类有时会更清晰,因为您会在 addListener 调用旁边看到代码,但它也会添加很多样板文件,如果您正在处理可能需要一段时间才能加载的非常大的项目,减少类的数量有时可以使其加载速度更快(每个匿名内部类都是 JVM 加载的另一件事)。

于 2011-12-19T13:26:50.817 回答