如何添加侦听多个按钮的动作侦听器

use*_*670 28 java swing jbutton actionlistener

我试图弄清楚我对动作听众做错了什么.我正在关注多个教程,但是当我尝试使用动作监听器时,netbeans和eclipse会给我错误.

下面是一个简单的程序,我试图让按钮工作.

我究竟做错了什么?

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


public class calc extends JFrame implements ActionListener {



    public static void main(String[] args) {

        JFrame calcFrame = new JFrame();

        calcFrame.setSize(100, 100);
        calcFrame.setVisible(true);

        JButton button1 = new JButton("1");
        button1.addActionListener(this);

        calcFrame.add(button1);
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == button1)
    }  

}
Run Code Online (Sandbox Code Playgroud)

动作监听器永远不会注册,因为if(e.getSource() == button1)它无法看到button1,错误说无法找到符号.

duf*_*ymo 37

this静态方法中没有指针.(我不相信这段代码甚至会编译.)

你不应该像静态方法那样做这些事情main(); 在构造函数中设置.我没有编译或运行它来查看它是否真的有效,但试一试.

public class Calc extends JFrame implements ActionListener {

    private Button button1;

    public Calc()
    {
        super();
        this.setSize(100, 100);
        this.setVisible(true);

        this.button1 = new JButton("1");
        this.button1.addActionListener(this);
        this.add(button1);
    }


    public static void main(String[] args) {

        Calc calc = new Calc();
        calc.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == button1)
    }  

}
Run Code Online (Sandbox Code Playgroud)


Qwe*_*rky 20

我很惊讶没有人提到使用动作命令.这是关联源和侦听器的非常标准的方式.它非常有用;

  • 你有多个事件源需要做同样的事情(例如,如果你希望使用能够按文本字段上的回车键作为单击旁边的按钮的替代方案)
  • 您没有对生成事件的组件的引用

看到;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;    
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class DontExtendJFrame implements ActionListener {

  private enum Actions {
    HELLO,
    GOODBYE
  }

  public static void main(String[] args) {

    DontExtendJFrame instance = new DontExtendJFrame();

    JFrame frame = new JFrame("Test");
    frame.setLayout(new FlowLayout());
    frame.setSize(200, 100);

    JButton hello = new JButton("Hello");
    hello.setActionCommand(Actions.HELLO.name());
    hello.addActionListener(instance);
    frame.add(hello);

    JButton goodbye = new JButton("Goodbye");
    goodbye.setActionCommand(Actions.GOODBYE.name());
    goodbye.addActionListener(instance);
    frame.add(goodbye);

    frame.setVisible(true);
  }

  @Override
  public void actionPerformed(ActionEvent evt) {
    if (evt.getActionCommand() == Actions.HELLO.name()) {
      JOptionPane.showMessageDialog(null, "Hello");
    } else if (evt.getActionCommand() == Actions.GOODBYE.name()) {
      JOptionPane.showMessageDialog(null, "Goodbye");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • @Lucas一般来说,人们应该赞成合成而不是继承. (4认同)
  • DontExtendJFrameAndDontImplementXXListenerAtTopLevel :-) (3认同)

And*_*son 9

以下是基于我的评论的源的修改形式.请注意,应该在EDT上构建和更新GUI,尽管我没有那么做.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JFrame;

public class Calc {

    public static void main(String[] args) {

        JFrame calcFrame = new JFrame();

        // usually a good idea.
        calcFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JButton button1 = new JButton("1");
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                JOptionPane.showMessageDialog(
                    button1, "..is the loneliest number");
            }
        });

        calcFrame.add(button1);

        // don't do this..
        // calcFrame.setSize(100, 100);

        // important!
        calcFrame.pack();

        calcFrame.setVisible(true);
    }
}
Run Code Online (Sandbox Code Playgroud)