如何调用不同的actionListeners?

时间:2011-11-10 15:05:50

标签: java swing actionlistener

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

  

textfield.addActionListener(this);
button.addActionListener(this);

...在我的构造函数中。

它们都执行相同的actionListener。我怎样才能调用各自的方法?

6 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:3)

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

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

2-In actionPerformed方法中,有一个ActionEvent类型的参数。调用它的 getSource 方法,并通过放置if语句检查它是否返回JTextFieldJButton的对象,并相应地分开。

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

当激活动作侦听器时,由于有人单击了您的按钮,因此调用方法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
  }
}

答案 4 :(得分:0)

虽然你没有提供样本代码,但我能理解那里有什么。

以下是如何向任何JComponent添加侦听器的示例。 (不要试图运行此代码!!!)

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

import javax.swing.JFrame;

public class EventListeners extends JFrame implements ActionListener {
    TextArea txtArea;
    String Add, Subtract, Multiply, Divide;
    int i = 10, j = 20, sum = 0, Sub = 0, Mul = 0, Div = 0;

    public void init() {
        txtArea = new TextArea(10, 20);
        txtArea.setEditable(false);
        add(txtArea, "center");
        Button b = new Button("Add");
        Button c = new Button("Subtract");
        Button d = new Button("Multiply");
        Button e = new Button("Divide");

        // YOU ARE DOING SOMETHING LIKE THIS
        // THIS WILL WORK, BUT CAN BE A BAD EXMPLE
        b.addActionListener(this);
        c.addActionListener(this);
        d.addActionListener(this);
        e.addActionListener(this);

        add(b);
        add(c);
        add(d);
        add(e);
    }

    public void actionPerformed(ActionEvent e) {
        sum = i + j;
        txtArea.setText("");
        txtArea.append("i = " + i + "\t" + "j = " + j + "\n");
        Button source = (Button) e.getSource();

        // you can work with them like shown below
                    Button source = (Button) e.getSource();
        if (source.getLabel() == "Add") {
            txtArea.append("Sum : " + sum + "\n");
        }
        if (source.getLabel() == "Subtract") {
            txtArea.append("Sub : " + Sub + "\n");
        }
        if (source.getLabel() == "Multiply") {
            txtArea.append("Mul = " + Mul + "\n");
        }
        if (source.getLabel() == "Divide") {
            txtArea.append("Divide = " + Div);
        }
    }
}

<强>更新

你应该做类似下面的事情

        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

答案 5 :(得分:0)

只需在每个组件上设置不同的ActionCommands即可 在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;
    }
}
相关问题