简体   繁体   English

Java - Swing - 。这意味着在JOptionPane中

[英]Java - Swing - .this meaning in JOptionPane

Consider following example ,please: 请考虑以下示例:

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

public class JavaWindow extends JFrame {

JButton button1;

public static void main(String[]args)
{
     new JavaWindow();
     // or JavaWindow Mwindow = new JavaWindow(); Question 1

}

public JavaWindow()
{   //setting window parameters etc. 
    this.setSize(500,500);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("My window");
      JPanel thePanel = new JPanel();
      button1 = new JButton("Get answer");
      ListenForButton lForButton = new ListenForButton();
      button1.addActionListener(lForButton);
      thePanel.add(button1);
      this.add(thePanel);
      this.setVisible(true);



}

private class ListenForButton implements ActionListener
{

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==button1)
        {
        JOptionPane.showMessageDialog(JavaWindow.this, "Hello!"); //JavaWindow.this - Question 2
        }
    }

}

} }

Question 1: In most of examples on the Internet creating an object that is supposed to be window is done this way "new JavaWindow();" 问题1:在Internet上的大多数示例中,创建一个应该是窗口的对象就是这样做的“new JavaWindow();” but isn't it equal to creating an object and naming it at the same time when we construct/create it but substituting "new JavaWindow();" 但它不等于创建一个对象并在我们构造/创建它时同时命名它,而是替换为“new JavaWindow();” with for example "JavaWindow MyWindow = new JavaWindow();" 例如“JavaWindow MyWindow = new JavaWindow();” ?

Question 2: Consider this line please : "JOptionPane.showMessageDialog(JavaWindow.this, "Hello!");". 问题2:请考虑这一行:“JOptionPane.showMessageDialog(JavaWindow.this,”Hello!“);”。 In this line JavaWindow.this is supposed to be parentComponent. 在这行JavaWindow.this应该是parentComponent。 In this case is the parentComponent: A) button1 - button that triggers the action (i guess No), B) Object (instance of class) ListenForButton that contains the method/function called actionPerformed and in this particular case this instance of the class (object of this type) is called lForButton (i am not sure but this is true), C) its the object created in main - the window we create (here: new JavaWindow()); 在这种情况下是parentComponent:A)button1 - 触发动作的按钮(我猜不是),B)Object(类的实例)ListenForButton包含名为actionPerformed的方法/函数,在这种特殊情况下,这个类的实例(这种类型的对象)被称为lForButton(我不确定,但这是真的),C)它在main中创建的对象 - 我们创建的窗口(这里:new JavaWindow()); ( I think No), D)Other answer??? (我认为不),D)其他答案???

Question 1: 问题1:

The difference between new JavaWindow(); new JavaWindow();之间的区别new JavaWindow(); and JavaWindow javaWindow = new JavaWindow(); JavaWindow javaWindow = new JavaWindow(); is that the latter you are storing a object of type JavaWindow in a variable, and with that way you can use the methods and public variables of that class using that object. 后者是将JavaWindow类型的对象存储在变量中,通过这种方式,您可以使用该对象使用该类的方法和公共变量。 The first one you can't, you just call the class constructors and that's it. 第一个你不能,你只需要调用类构造函数就可以了。 Well, you can do new JavaWindow().methodName() but then you wouldn't be able to, for example, give it as an argument in a method that requests JavaWindow type. 好吧,您可以执行new JavaWindow().methodName()但是您将无法在请求JavaWindow类型的方法中将其作为参数提供。

Summarizing, only create an instance of a class if you want to use it later or differ to others, in your case you don't then just new JavaWindow() is valid. 总结一下,如果你想稍后使用它或者与其他人不同,那么只创建一个类的实例,在你没有这种情况下你只需要new JavaWindow()是有效的。

Question 2: 问题2:

parentComponent is the main component that you want to JOptionPanel appear. parentComponent是您希望JOptionPanel出现的主要组件。 In that case is your JFrame JavaWindow . 在这种情况下是你的JFrame JavaWindow The reason it's not just this and yes JavaWindow.this is because the statement is inside an inner class, and this would refer to that class. 它不仅仅是this而且是JavaWindow.this的原因是因为语句在一个内部类中, this将引用该类。 Thefore JavaWindow.this to correctly refer to the outer class JavaWindow . 在JavaWindow.this JavaWindow.this正确引用外部类JavaWindow

Regarding your Question 2: 关于你的问题2:

Since the line JOptionPane.showMessageDialog(JavaWindow.this, "Hello!"); JOptionPane.showMessageDialog(JavaWindow.this, "Hello!"); is in a method declared within the class ListenForButton , this would refer to the ListenForButton instance. ListenForButton类中声明的方法中, this将引用ListenForButton实例。 This is an ActionListener , but not a Component , as required by JOptionPane.showMessageDialog() . 这是一个ActionListener ,但不是JOptionPane.showMessageDialog()所要求的Component

Since the class ListenForButton is an inner class of JavaWindow , you have access to the instance of the enclosing JavaWindow object through the notation JavaWindow.this , so your answer C is the correct answer. 由于类ListenForButtonListenForButton的内部类, JavaWindow您可以通过符号JavaWindow.this访问封闭的JavaWindow对象的实例,因此您的答案C是正确的答案。

1. You are creating object using new JavaWindow(); 1.您正在使用new JavaWindow();创建对象new JavaWindow(); but not storing it in a variable. 但不要将其存储在变量中。 In other words you can say that the object that you have created is not stored in any reference variable as such. 换句话说,您可以说您创建的对象不会存储在任何引用变量中。

2. This line simply says " Show a message dialog box with message Hello! in the current instance of JFrame (JavaWindow.this) " 2.这行简单地说“ 在JFrame的当前实例(JavaWindow.this)中显示带有消息Hello!的消息对话框

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM