什么是“这个”这个程序意味着什么? [英] What does "this" mean in this program?

查看:104
本文介绍了什么是“这个”这个程序意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,然后我在这个代码中使用这个关键字的网站上看到了这个,我想知道它的用途是什么,它可以处理一个Jbutton或JTextField,它可以使用this关键字显示消息, getSource()发生了什么?

I was writing a program, and then I saw this on some website that is using the this keyword in this code, and I was wondering what it's purpose, it can process a Jbutton, or JTextField, it can show message using the this keyword, what happened to getSource()?

这是代码

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

public class TextPassWordEvent extends JFrame {

    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JPasswordField passwordField;
    private JButton button;

    public TextPassWordEvent(){
        super("Title");
        setLayout(new FlowLayout());

        textField1 = new JTextField(10);
        add(textField1);

        textField2 = new JTextField("Enter your Text Here");
        add(textField2);

        textField3 = new JTextField("Uneditable Text field");
        textField3.setEditable(false);
        add(textField3);

        passwordField = new JPasswordField("Password");
        add(passwordField);

        button = new JButton("Submit");
        add(button);

        TextHandler handler = new TextHandler();
        textField1.addActionListener(handler);
        textField2.addActionListener(handler);
        textField3.addActionListener(handler);
        passwordField.addActionListener(handler);
        button.addActionListener(handler);
    }

    private class TextHandler implements ActionListener{

        public void actionPerformed(ActionEvent event){
            JOptionPane.showMessageDialog(TextPassWordEvent.this, String.format("Message: %s",event.getActionCommand()));
        }
    }

}


推荐答案

在java中,这指的是当前的对象实例。

在上面的例子中,使用'this'的地方是在内部类的一些代码中。如果此代码使用'this'而没有任何上下文,那么它将引用内部类的实例。

In java this refers to the current object instance.
In the example above, the place where 'this' is used is in some code within an inner class. If this code used 'this' without any context, then it would be referring to the instance of the inner class.

Java提供了OuterClassName.this的表示法,作为引用此内部类实例化的外部类实例的一种方式。

Java provides the notation of OuterClassName.this as a way of referring to the instance of the outer class that this inner class was instantiated in.

所以它只是引用TextPasswordEvent类的实例。

So its simply referring to the instance of the TextPasswordEvent class.

TextPasswordEvent是一个容器,当调用showMessageDialogue时,你需要传递一个容器,在该容器中将显示对话框,因此TextPasswordEvent.this说传递实例TextPasswordEvent对象到showMessageDialogue方法。如果它没有使用限定条件并且只是传递了this,那么它将传递对TextHandler内部类实例的引用,而不是所期望的行为。

TextPasswordEvent is a container and when calling showMessageDialogue you need to pass a container within which the dialog will be displayed, so the TextPasswordEvent.this is saying "pass the instance of the TextPasswordEvent object" to the showMessageDialogue method. If it didn't use the qualification and just passed "this" it would be passing a reference to the TextHandler inner class instance instead which is not the desired behaviour.

编辑:更多信息

TextHandler是一个实现ActionListener接口的内部类。在此接口中,有一个名为actionPerformed的方法。

TextHandler is an inner class that implements the ActionListener interface. In this interface there is a method defined called actionPerformed.

外部类正在创建各种控件(按钮,文本字段等),然后创建TextHandler类的一个实例并将其设置为这些控件上的动作侦听器。然后,当用户按下按钮或返回键(取决于控件)时,这些控件将调用actionPerformed方法。

The outer class is creating various controls (buttons, text field etc.) and then creating one instance of the TextHandler class and setting it as the action listener on these controls. These controls then call the actionPerformed method when the user hits a button or the return key (depending on the control).

在内部类的actionPerformed方法中,它显示一个对话框(showMessageDialogue)并传递两个参数 - 用于显示内部对话框和要在其中显示的消息的容器。

Inside the actionPerformed method of the inner class its showing a dialog (showMessageDialogue) and passing two params - the container to show the dialog inside and the message to display in it.

它显示的消息包括传递的命令。这是通过在传递给actionPerformed()方法的ActionEvent对象上调用getActionCommand()获得的。在用户完成操作(按下按钮,点击返回键等)后,控件在调用actionPerformed时创建并传递此ActionEvent对象。

The message it is showing is including the command that was passed. This is obtained by calling getActionCommand() on the ActionEvent object that is passed to the actionPerformed() method. The controls create and pass this ActionEvent object when they make the call to actionPerformed, after a user has done the action (pressed the button, hit the return key etc.).

编辑2:

为另一个只添加到按钮的ActionListener添加新的内部类定义组件:

Add a new inner class definition for another ActionListener that will only be added to your button component:

private class MyButtonHandler implements ActionListener{

    public void actionPerformed(ActionEvent event){
        JOptionPane.showMessageDialog(TextPassWordEvent.this, "This only gets shown when the button gets pressed because I only added an instance of this action listener to button and none of the other components");
    }
}

然后返回TextPassWordEvent构造函数代码:

Then back in the TextPassWordEvent constructor code:

TextHandler handler = new TextHandler();
textField1.addActionListener(handler);
textField2.addActionListener(handler);
textField3.addActionListener(handler);
passwordField.addActionListener(handler);

// Create an instance of our button handler and add that to our button instead of the
// other handler.
MyButtonHandler buttonHandler = new MyButtonHandler();
button.addActionListener(buttonHandler);

这篇关于什么是“这个”这个程序意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆