ActionListener 接口

ActionListener interface

public class myWindow extends JFrame implements ActionListener{

如果我有此代码,我的 class 将是一个 JFrame,我可以在其中添加组件并在我的构造函数中向它们添加 actionlistener,如下所示

public MyWindow()
     {
      JButton b = new Jbutton("button");
       b.addActionListener(this);
        }

此关键字将用作匿名动作侦听器 object(这是我的 class)对吗?

稍后我将使用以下标题覆盖 actionPerformed 方法:-

 public void ActionPerformed(ActionEvent ae)
         {      : 
                :
                        }

我真的很困惑..我的书上说 "the listener object invokes an event handler method with the event as an argument "

听众object:这个

事件处理方法:ActionPerformed(ActionEvent ae)

参数:我的事件是 JButton b .. 为什么不是 EventAction 类型?如果是这样,我们为什么要使用 :

 ae.getActionCommand(); 

我认为这是一种判断哪个组件触发事件的方法,为什么当组件作为参数传递时我们需要它?

您的 JButton 是组件而不是事件。在这种情况下,事件是由组件上的某些操作生成的 当您单击按钮时,将触发 ActionEvent 并将其传递给在这种情况下订阅该事件的所有侦听器是用作 ActionListener

MyWindow 对象

这个关键字将用作匿名 actionlistener 对象(这是我的 class)对吗?

不,这将是 class 的对象,它正在实现 actionsListener。在你的情况下是 "MyWindow".

我的事件是 JButton b .. 为什么不是 EventAction 类型?如果是这样,为什么我们使用:

JButton b 是组件而不是事件。事件描述源状态的变化。当用户与 GUI 交互时,会生成事件,如单击按钮、移动鼠标​​。

来自点击的引用 here

事件处理是一种控制事件并决定事件发生时应该发生什么的机制。

事件处理涉及的步骤:-

  1. 用户点击按钮,事件产生。

  2. 现在自动创建相关事件 class 的对象,并在同一对象中填充有关源和事件的信息。

  3. 事件对象转发给注册监听器的方法class.

  4. 方法现在被执行并且returns.

我认为这是一种判断哪个组件触发事件的方法,为什么当组件作为参数传递时我们需要它?

现在您应该明白了,可以有许多按钮注册到同一个 ActionsListerner。现在为不同的事件执行不同的动作, e.getActionCommand() 派上用场,它会告诉您哪个按钮是触发事件的来源。 希望这有帮助。

我已尝试为您提供一个简单的 JButton 程序示例。

    import javax.swing.*;       
    import java.awt.*;
    import java.awt.event.*;

    public class ButtonSwing {
        private int numClicks = 0;

        public Component createComponents() {
            //Method for creating the GUI componenets
            final JLabel label = new JLabel("Clicks: " + "0"); //final so that i can access inside inner class
            JButton button = new JButton("Simple Button");
            button.addActionListener(
            //inner class for ActionListener. This is how generally it is done.     
            new ActionListener() {
                           public void actionPerformed(ActionEvent e) {
                               numClicks++;
                               label.setText("Clicks: " + numClicks);
                   System.out.println(e.getActionCommand());
                   System.out.println(e.getModifiers());
                   System.out.println(e.paramString());
             }
            }
           );
            JPanel pane = new JPanel();   //using JPanel as conatiner first.
            pane.setLayout(new FlowLayout());
            pane.add(button);  // adding button to the JPanel.
            pane.add(label);   // adding label to the JPanel.
            return pane;
        }

        public static void main(String[] args) {
            JFrame frame = new JFrame("SwingApplication");
            ButtonSwing obj = new ButtonSwing();
            Component contents = obj.createComponents();
            frame.getContentPane().add(contents);
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            frame.pack();  //It will cause the window to be sized to fit the preferred size 
                            //and layouts of its subcomponents. 
            frame.setVisible(true);
        }
    }