Failing to properly Close Windows
On my journey through my java Book, I'm starting to learn about frames.
When you click the "X" to close the window, this program does not completely terminate for some reason, you have to use ctrl^c in the console to exit it. I do not understand why, unless this approach is deprecated. Please Help.
WindowClosingTest.java
Code Java:
// WindowClosingTest.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class WindowClosingTest extends JFrame
{
JTextArea frameTextArea;
Container frameContainer;
public WindowClosingTest()
{
super("WindowClosingTest Application");
init();
}
public void init()
{
addWindowListener(new WindowCloser());
frameContainer = getContentPane();
frameTextArea = new JTextArea("\t Window Closing Test Class.", 50, 50);
frameTextArea.setEditable(true);
frameTextArea.setLineWrap(true);
frameContainer.add(frameTextArea);
setSize(520, 300);
setVisible(true);
}
public static void main(String Args[])
{
new WindowClosingTest();
}
}//end class
WindowCloser.java
Code Java:
//WindowCloser.java
import java.awt.event.*;
import javax.swing.*;
public class WindowCloser extends WindowAdapter
{
public WindowCloser(){}
public void windowClosing(WindowEvent wE)
{
System.exit(0);
}
}//end class
Re: Failing to properly Close Windows
nevermind I got it, (WindowEvent wE) should be (WindowEvent e). Why the the book had it as "wE" and not "e" i'll never know.
Re: Failing to properly Close Windows
a) wE or e, it should not matter as long as when you access the variable inside the method you use the appropriate name b) Read the API for JFrame, you are better off using setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)
Re: Failing to properly Close Windows
A potential problem when you extend an adapter class is that if you misspell the method you want to override, the compiler is very happy to add that method as a new one vs overriding another.
Use the @Override statement just before your method definition to have the compiler check if your method is an override.