Consider the following example:
import javax.swing.*;
import java.awt.event.*;
public class JavaWindow extends JFrame {
JButton button1;
public static void main(String[]args)
{
new JavaWindow();
}
public JavaWindow()
{
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!");
}
}
}
}
Question 1: In most examples on the Internet, an object is created, which should be, the window is made in this way "new JavaWindow ();" but right? equal to creating an object and naming it at the same time when we create / create it, but replace "new JavaWindow ()"; for example, "JavaWindow MyWindow = new JavaWindow ();"
2. : "JOptionPane.showMessageDialog(JavaWindow.this," Hello! ");".
JavaWindow.this parentComponent.
parentComponent:
A) button1 - , ( , ),
B) ( ) ListenForButton, /, actionPerformed, ( ) lForButton ( , ),
C) , - (: new JavaWindow()); ( ),
D) ???