Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何调用不同的ActionListener?_Java_Swing_Actionlistener - Fatal编程技术网

Java 如何调用不同的ActionListener?

Java 如何调用不同的ActionListener?,java,swing,actionlistener,Java,Swing,Actionlistener,我的程序有一个按钮,另一个是JTextField。按钮和文本字段的操作侦听器不同。我正在使用: textfield.addActionListener(这个) addActionListener(这个) 。。。在我的构造函数中 他们都做同样的动作。如何调用它们各自的方法?显然,这两个组件共享一个ActionListener。如果要确定哪个组件生成了ActionEvent,请调用。从那里,您可以进行类型转换(如果需要),然后调用特定组件的方法。显然,这两个组件共享一个ActionListener。

我的程序有一个按钮,另一个是JTextField。按钮和文本字段的操作侦听器不同。我正在使用:

textfield.addActionListener(这个)
addActionListener(这个)

。。。在我的构造函数中


他们都做同样的动作。如何调用它们各自的方法?

显然,这两个组件共享一个
ActionListener
。如果要确定哪个组件生成了
ActionEvent
,请调用。从那里,您可以进行类型转换(如果需要),然后调用特定组件的方法。

显然,这两个组件共享一个
ActionListener
。如果要确定哪个组件生成了
ActionEvent
,请调用。从那里,您可以进行类型转换(如果需要),然后调用特定组件的方法。

当某个操作侦听器被激活时,由于有人单击了您的按钮,将调用方法
actionPerformed
。当您将
this
设置为动作监听器时,您应该在类中有一个方法
actionPerformed
。这是两种情况下调用的方法

比如:

class MyClass implements ActionListener {

  public MyClass() {
    ...
    textfield.addActionListener(this) ;
    button.addActionListener(this) ;
    ...
  }

  public void actionPerformed(ActionEvent e) {
    // This is the method being called when:
    // - the button is clicked and
    // - the textfield activated
  }
}

当某个操作侦听器被激活时,由于有人单击了您的按钮,将调用方法
actionPerformed
。当您将
this
设置为动作监听器时,您应该在类中有一个方法
actionPerformed
。这是两种情况下调用的方法

比如:

class MyClass implements ActionListener {

  public MyClass() {
    ...
    textfield.addActionListener(this) ;
    button.addActionListener(this) ;
    ...
  }

  public void actionPerformed(ActionEvent e) {
    // This is the method being called when:
    // - the button is clicked and
    // - the textfield activated
  }
}

您正在这两个组件的类中实现
ActionListener
。因此,当一个动作发生时,会为这两个动作调用类的
actionPerformed
方法。您可以执行以下操作将它们分开:

1-创建一个单独的类,并在其中实现
ActionListener
接口,并将其添加为其中一个组件的ActionListener


2-在
actionPerformed
方法中,有一个类型为
ActionEvent
的参数。调用它的getSource方法,检查它是否返回
JTextField
JButton
的对象,方法是放置一个if语句,并相应地执行不同的操作

您正在两个组件的类中实现
ActionListener
。因此,当一个动作发生时,会为这两个动作调用类的
actionPerformed
方法。您可以执行以下操作将它们分开:

1-创建一个单独的类,并在其中实现
ActionListener
接口,并将其添加为其中一个组件的ActionListener


2-在
actionPerformed
方法中,有一个类型为
ActionEvent
的参数。调用它的getSource方法,检查它是否返回
JTextField
JButton
的对象,方法是放置一个if语句,并相应地执行不同的操作

虽然如果您没有给出示例代码,但我可以理解其中的内容

下面是一个如何将侦听器添加到任何
JComponent
的示例。(不要尝试运行此代码!!!)

更新

你应该像下面这样做

        Button b = new Button("Add");
        Button c = new Button("Subtract");
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // implement what is expected for b button 
            }
        });
        c.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // implement what is expected for c button 
            }
        });
        // and so on...
        // but yes we can improve it

虽然您没有给出示例代码,但我可以理解其中的内容

下面是一个如何将侦听器添加到任何
JComponent
的示例。(不要尝试运行此代码!!!)

更新

你应该像下面这样做

        Button b = new Button("Add");
        Button c = new Button("Subtract");
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // implement what is expected for b button 
            }
        });
        c.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // implement what is expected for c button 
            }
        });
        // and so on...
        // but yes we can improve it

只需在每个组件上设置不同的操作命令。
在actionPerformed方法中,可以检查事件的ActionCommand:

private static final String TEXT_CMD = "text";  // or something more meaningful
private static final String BUTTON_CMD = "button";

...

    textfield.setActionCommand(TEXT_CMD);
    textfield.addActionListener(this);

    button.setActionCommand(BUTTON_CMD);
    button.addActionListener(this);

...

public void actionPerformed(ActionEvent ev) {
    switch (ev.getActionCommand()) {
        case TEXT_CMD:
            // do textfield stuff here
            break;
        case BUTTON_CMD:
            // do button stuff here
            break;
        default:
            // error message?
            break;
    }
}

只需在每个组件上设置不同的操作命令。
在actionPerformed方法中,可以检查事件的ActionCommand:

private static final String TEXT_CMD = "text";  // or something more meaningful
private static final String BUTTON_CMD = "button";

...

    textfield.setActionCommand(TEXT_CMD);
    textfield.addActionListener(this);

    button.setActionCommand(BUTTON_CMD);
    button.addActionListener(this);

...

public void actionPerformed(ActionEvent ev) {
    switch (ev.getActionCommand()) {
        case TEXT_CMD:
            // do textfield stuff here
            break;
        case BUTTON_CMD:
            // do button stuff here
            break;
        default:
            // error message?
            break;
    }
}

对我来说,最简单的方法是:

textfield.addActionListener(this);
button.addActionListener(this);
...
public void actionPerformed(ActionEvent e){
    if( e.getSource().getClass().equals(JTextField.class) ){
        System.out.println("textfield");
        //Código para el textfield
    }
    if( e.getSource().getClass().equals(JButton.class) ){
        System.out.println("JButton");
        //Código para el JButton
    }
}

对我来说,最简单的方法是:

textfield.addActionListener(this);
button.addActionListener(this);
...
public void actionPerformed(ActionEvent e){
    if( e.getSource().getClass().equals(JTextField.class) ){
        System.out.println("textfield");
        //Código para el textfield
    }
    if( e.getSource().getClass().equals(JButton.class) ){
        System.out.println("JButton");
        //Código para el JButton
    }
}

最好链接到最新版本的JavaDocs。我已将您的答案编辑为指向J2SE 7。有关获取最新文档链接的提示,请参阅。嗯,链接不起作用。不过,我想我可能知道你在说什么。我将尝试用if-else语句修复代码:)谢谢~@alicedimarco,链接已修复;很抱歉最好链接到最新版本的JavaDocs。我已将您的答案编辑为指向J2SE 7。有关获取最新文档链接的提示,请参阅。嗯,链接不起作用。不过,我想我可能知道你在说什么。我将尝试用if-else语句修复代码:)谢谢~@alicedimarco,链接已修复;很抱歉谢谢:)这也是我在浏览完评论后的想法~我认为这只是在这种情况下的可怕做法。。对于许多用例,您可能会发现它非常有用并且是最佳实践。假设您有数百个面板,每个面板中都有一个特定的按钮,可以在新对话框中打开文本字段中写入的文本。在这个单独的类中,您可以存储相关的textfield并打开对话框。因此,如果你在一个有数百个面板的系统中有一项针对多个组件的特定工作,那么你必须使用第一个选项…谢谢:)这也是我在浏览评论后想到的~我认为仅在这种情况下这是可怕的做法。。对于许多用例,您可能会发现它非常有用并且是最佳实践。假设您有数百个面板,每个面板中都有一个特定的按钮,可以在新对话框中打开文本字段中写入的文本。在这个单独的类中,您可以存储相关的textfield并打开对话框。因此,如果在一个有数百个面板的系统中,有一个特定的作业用于多个组件,那么您必须使用第一个选项…噢。这也很好。谢谢!:)哦。这也很好。谢谢!:)请只使用英语。请只使用英语。这是我的个人安慰