简体   繁体   中英

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();" 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();" with for example "JavaWindow MyWindow = new JavaWindow();" ?

Question 2: Consider this line please : "JOptionPane.showMessageDialog(JavaWindow.this, "Hello!");". In this line JavaWindow.this is supposed to be 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()); ( I think No), D)Other answer???

Question 1:

The difference between new JavaWindow(); and 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. 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.

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.

Question 2:

parentComponent is the main component that you want to JOptionPanel appear. In that case is your 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. Thefore JavaWindow.this to correctly refer to the outer class JavaWindow .

Regarding your Question 2:

Since the line JOptionPane.showMessageDialog(JavaWindow.this, "Hello!"); is in a method declared within the class ListenForButton , this would refer to the ListenForButton instance. This is an ActionListener , but not a Component , as required by JOptionPane.showMessageDialog() .

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.

1. You are creating object using 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) "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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