Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing:动作监听器的角色_Java_Swing - Fatal编程技术网

Java Swing:动作监听器的角色

Java Swing:动作监听器的角色,java,swing,Java,Swing,#在我发布的问题中,我无法理解第1行和第2行中提到的代码,因为我知道它们用于设置按钮的动作侦听器,但最让我困惑的是,在第1行和第2行的sated语法中,{JB1.addActionListener(this)}在这篇文章中,“this”的作用是什么……所以请详细地告诉我们它背后的基本原理,以及整个语法是如何工作的# < > >强> 1。< /强>认为侦听器是对某个动作反应的人 > 2.ActionListener是一个接口,它有一个回调方法,名为actionPerformed,其中是在控制器上

#在我发布的问题中,我无法理解第1行和第2行中提到的代码,因为我知道它们用于设置按钮的动作侦听器,但最让我困惑的是,在第1行和第2行的sated语法中,{JB1.addActionListener(this)}在这篇文章中,“this”的作用是什么……所以请详细地告诉我们它背后的基本原理,以及整个语法是如何工作的#


< > >强> 1。< /强>认为侦听器是对某个动作反应的人<代码> >

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

3.此行
JB1.addActionListener((ActionListener)This)的意思如下

   JB1 - Button
   ActionListener - Interface
   addActionListener - Registering the Button with the Listener.
4.
addActionListener
绑定/注册
按钮
监听器
(此处为其ActionListener)

5.
MVC
体系结构
中,按钮是控制器
,当对其执行某些操作时,通过将其注册到侦听器,通知所有人执行某些操作

6。并且此侦听器将具有
回调
方法,该方法将被实现侦听器的类覆盖。

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

“this”指的是封闭类(frametest1_1)的当前实例,该实例是在run()方法中构造的

关于“这个”的进一步解读:

从面向对象设计的角度来看,提供的代码非常难看,因此您会感到困惑。让一个班级负责以下工作绝对不是个好主意:

  • 包含主方法
  • 保留对
    JFrame
    组件的引用(即
    JLabel
  • ActionListener
    概念的实现
因此,尽管这些丑陋的例子甚至可以在Swing的官方教程中找到

以下是完全相同功能的稍微好一点的实现:


希望这有助于理解…

请学习java命名约定并遵守它们。谢谢…。但请让我知道一件事,我传递的参数“this”基本上是类frametest1_1的实例。如果我在创建该类后传递该类的对象,如frametest1_1 obj=new frametest1_1();,而不是“this”;,如果“this”在这里的意思是“current object”,那么您传递的是实现了Runnable接口的类的实例,您可以传递该类的对象来代替它,但是您必须确保您正在实例化
与您从main()函数
将侦听器附加到的同一个对象。当您应用单例模式时,在这种情况下可能只创建一个对象。通过使用初始化GUI的静态方法提供一个反OO模式,您正在提供OO课程。您应该拥有一个实例,该实例本身在构造函数或实例方法中创建GUI<代码>静态
是面向对象编程最糟糕的一种方式!我同意,从OO的角度来看,我的代码并不是100%完美,尽管我只是试图解释如何在Swing中正确使用
ActionListener
s,而不是一般地解释如何构造基于Swing的应用程序。关于这里使用的
static
方法,我在这里使用它有两个原因:1。此示例太小,无法在此处创建单独的类/实例2。为了不污染
main
方法的逻辑,你回答的第一句话:“从面向对象设计的角度来看,提供的代码非常难看”,所以我假设你提供的东西从面向对象设计的角度来看并不难看。其次,如果你认为你的例子太小而不能遵循面向对象的设计,那么这个问题就和你的答案一样有效。最后,从MVC的角度来看,这是完全可以接受的。您有一个控制器类(frametest_1_1),它创建视图并侦听视图事件。我认为用户主要对
this
ActionListener
的用法感到困惑,从
MVC
的角度来看,创建视图的
控制器是完全可以接受的?我认为没有人关心带有
静态
的示例是否是为一个简短的演示编写的
main(String[]args)
在这种情况下既不是我的敌人,也不是OOP的敌人,对不起。
   JB1 - Button
   ActionListener - Interface
   addActionListener - Registering the Button with the Listener.
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
    }

}