0

# 在我发布的问题中,我无法理解第 1 行和第 2 行提到的代码,据我所知,它们用于设置按钮的动作侦听器,但实际上是对我来说最令人困惑的是,在第 1 行和第 2 行的语法中,{JB1.addActionListener(this)} 在此中“this”的作用是什么.....所以请说出这背后的基本原理。以及整个语法是如何工作的……详细说明。#

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

    import javax.swing.*;



    public class frametest1_1 implements ActionListener
    {
        JLabel JL;
        public frametest1_1()
        {
            //create a JFrame container
            JFrame JF=new JFrame("A BUTTON");

            //Frame Layout This is contained in Java .awt.*;  "ON USING THIS OPTION OUR BUTTON AND OTHER COMPONENT ARE ADJUSTED IN THE FRAME AUTOMATICALLY"
            JF.setLayout(new FlowLayout());

            //set the size of the container
            JF.setSize(200, 200);

            //set visible
            JF.setVisible(true);

            //make button
            JButton JB1=new JButton("FIRST");
            JButton JB2=new JButton("SECOND");

            //add button to the JFrame container
            JF.add(JB1);
            JF.add(JB2);

            //Create and add Label to the JFrame container
            JL=new JLabel("PRESS A BUTTON");
            JF.add(JL);

            //set action command :now this will help in determining that which button is presses, is it FIRST or SECOND
            JB1.setActionCommand("one");
            JB2.setActionCommand("two");

            //The action responded is added to the actionlistener
            JB1.addActionListener((ActionListener) this); // line 1
            JB2.addActionListener((ActionListener) this); // line 2
        }   

        public void actionPerformed(ActionEvent ae) 
        {

            if(ae.getActionCommand().equals("one"))
                JL.setText("First Button Pressed");     // to set text on the label
            else
                JL.setText("Second button Pressed");    // to set the text on the label

        }
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new frametest1_1();
                }
            });
        }
    }
4

3 回答 3

1

1.考虑一下Listener is someone who reacts to some action

2.ActionListener是一个接口,它有一个回调方法,名为actionPerformed,里面是在控制器上执行某些操作时将运行的代码。

3.这条线JB1.addActionListener((ActionListener) this);的意思如下

   JB1 - Button
   ActionListener - Interface
   addActionListener - Registering the Button with the Listener.

4. addActionListener绑定/注册Button到(Listener这里是它的ActionListener)。

5.MVC架构Button is the controller中,当对其执行某些操作时,通过将其注册给侦听器来通知谁来执行某些操作。

6.这个监听器将有一个方法,该callback方法将被实现监听器的类覆盖。

7.此外,在您的示例中,您也可以这样做... JB1.addActionListener(this);

于 2012-07-24T11:20:23.850 回答
1

'this' 指的是封闭类 (frametest1_1) 的当前实例,即在 run() 方法中构造的实例。

关于“this”的一些进一步阅读: 使用 this 关键字

于 2012-07-24T11:21:38.220 回答
-1

从面向对象设计的角度来看,提供的代码非常丑陋 - 因此您会感到困惑。让一个班级负责:

  • 包含主要方法
  • 持有对JFrame组件的引用(即JLabel
  • ActionListener概念的实施

因此违反了单一责任原则,尽管在 Swing 的官方教程中也可以找到这样丑陋的例子。

完全相同功能的稍微更好的实现如下:

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

import javax.swing.*;

public class frametest1_1 {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                runFrameTest();
            }
        });
    }

    public static void runFrameTest() {
        //create a JFrame container
        JFrame JF=new JFrame("A BUTTON");

        //Frame Layout This is contained in Java .awt.*;  "ON USING THIS OPTION OUR BUTTON AND OTHER COMPONENT ARE ADJUSTED IN THE FRAME AUTOMATICALLY"
        JF.setLayout(new FlowLayout());

        //set the size of the container
        JF.setSize(200, 200);

        //set visible
        JF.setVisible(true);

        //make button
        JButton JB1=new JButton("FIRST");
        JButton JB2=new JButton("SECOND");

        //add button to the JFrame container
        JF.add(JB1);
        JF.add(JB2);

        //Create and add Label to the JFrame container
        final JLabel JL=new JLabel("PRESS A BUTTON");
        JF.add(JL);

        //set action command :now this will help in determining that which button is presses, is it FIRST or SECOND
        JB1.setActionCommand("one");
        JB2.setActionCommand("two");

        ActionListener labelUpdater = new ActionListener() {
            public void actionPerformed(ActionEvent ae) {               
                if(ae.getActionCommand().equals("one"))
                    JL.setText("First Button Pressed");     // to set text on the label
                else
                    JL.setText("Second button Pressed");    // to set the text on the label             
            }
        };
        //The action responded is added to the actionlistener
        JB1.addActionListener(labelUpdater); // line 1
        JB2.addActionListener(labelUpdater); // line 2
    }

}

希望这有助于理解...

于 2012-07-24T11:58:51.007 回答