Swing:动作监听器的作用

时间:2012-07-24 11:03:16

标签: java swing

#在我发布的问题中,在这里我无法理解第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();
                }
            });
        }
    }

3 个答案:

答案 0 :(得分: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 绑定/注册 ButtonListener(此处为其ActionListener)。

5。MVC架构Button is the controller中,当对其执行某些操作时,通过注册来完成所有人通知他们做某些事情听众。

6。此监听器将使用callback方法,该方法将被实现侦听器的类覆盖。

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

答案 1 :(得分:1)

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

关于'this'的进一步阅读: Using the this Keyword

答案 2 :(得分:-1)

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

,这绝对是个坏主意
  • 包含主要方法
  • 持有对JFrame组件的引用(即JLabel
  • ActionListener概念
  • 的实施

因此违反了Single responsibility principle,尽管在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
    }

}

希望这有助于理解......