In fact, this is originally a basic topic about Java, but since some people come out, I may wish to say two sentences, and by the way, it can be used as a reference for beginners.
// 1. Maximize //frame1.java at the time of the form
Import java.awt. *; import java.awt.event. *; import javax.swing.uimanager;
public class Frame1 extends WindowAdapter {public Frame1 () {Frame f = new Frame (); f.addWindowListener (this); // Frame1 the event handler is f Dimension screenSize = Toolkit.getDefaultToolkit () getScreenSize ().; // Get the size of the screen F.SetLocation (0, 0); F.setsize (Screensize.Width, Screensize.Height); // Set the size of the form as the screen size f.setvisible (TRUE);} public static Void main (String [] args) {new frame1 ();} public void window {system.exit (0);}}
// 2. Close the six methods of the form //2.1. Enableevents and processWindowEvent // frame1.java using JFrame
Import java.awt. *; import java.awt.event. *; import javax.swing. *;
Public class frame1 extends jframe {public frame1 () {enableevents (awtevent.window_event_mask); this.setsize (New Dimension (400, 300)); this.Settitle ("frame1");}
Protected Void ProcessWindowEvent (WindowEvent E) {Super.ProcessWindOwevent (E); if (E.GetId () == WINDOWEVENT.WINDOW_CLOSIING) {system.exit (0);}}}
//2.2. Directly implement WindowListener interface //frame1.java
Import java.awt. *; import java.awt.event. *;
Public class frame1 extends frame imports windowlistener {public frame1 () {this.setsize (New Dimension (400, 300)); this.Settitle ("frame1"); this.addwindowlistener (this);}
public void windowClosing (WindowEvent windowEvent) {System.exit (0);} public void windowOpened (WindowEvent windowEvent) {} public void windowClosed (WindowEvent windowEvent) {} public void windowIconified (WindowEvent windowEvent) {} public void windowDeiconified (WindowEvent windowEvent) {} Public void windowactivated (windowevent windowevent) {} public void windowevent {}} // 2.3. Direct inheritance Form Adapter Windowadapter // Frame1.java
Import java.awt. *; import java.awt.event. *;
Public class frame1 extends windowadapter {public frame1 () {frame f = new frame (); f.setsize (new Dimension (400, 300)); F.SetTitle ("frame1"); f.addwindowlistener (this); f. SetVisible (TRUE); public static void main (String [] s) {new frame1 ();} public void windowevent {system.exit (0);}}
//2.4. Indirect inheritance Form Adapter WINDOWADAPTER / / FRME1.JAVA
Import java.awt. *; import java.awt.event. *;
Public class frame1 () {this.setsize (new Dimension (400, 300)); this.SetTitle ("frame1"); this.addwindowlistener (new WinAdapter ()); this.setVisible (TRUE); } Public static void main (String [] s) {new frame1 ();}} class winadapter extends windowadapter {public void windowclosing (windowevent windowevent) {system.exit (0);}}
//2.5. Indirect implementation of Windowlistener interface //frame1.java
Import java.awt. *; import java.awt.event. *;
Public class frame1 extends frame {public frame1 () {this.setsize (New Dimension (400, 300)); this.SetTitle ("frame1"); this.addwindowlistener (New WineventHandle ()); this.setVisible (TRUE); } public static void main (String [] s) {new Frame1 ();}} class winEventHandle implements WindowListener {public void windowClosing (WindowEvent windowEvent) {System.exit (0);} public void windowOpened (WindowEvent windowEvent) {} public void windowClosed (WindowEvent windowEvent) {} public void windowIconified (WindowEvent windowEvent) {} public void windowDeiconified (WindowEvent windowEvent) {} public void windowActivated (WindowEvent windowEvent) {} public void windowDeactivated (WindowEvent windowEvent) {}} // 2.6. use Inner Class // Frame1.java
Import java.awt. *; import java.awt.event. *;
public class Frame1 {public Frame1 () {Frame f = new Frame (); f.addWindowListener (new WindowAdapter () {public void windowClosing (WindowEvent e) {System.exit (0);}}); f.setSize (new Dimension (400, 300)); f.setvisible (true);
Public static void main (string [] s) {new frame1 ();}}