Saturday, August 2, 2008

Java runtime maximize form and closed methods example

In fact, this was originally the basis of a java-related topics, but since it was raised, we might as well say a few words, the way also can be used as a reference for beginners.

/ / 1. Maximize form startup
/ / Frame1.java

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); / / f Frame1 set to deal with the incident
Dimension screenSize = Toolkit.getDefaultToolkit (). GetScreenSize () / / by the size of the screen
F.setLocation (0, 0);
F.setSize (screenSize.width, screenSize.height) / / set form the size of the size of the screen
F.setVisible (true);
)

Public static void main (String [] args) (
New Frame1 ();
)

Public void windowClosing (WindowEvent e) (
System.exit (0);
)
)

/ / 2. Closed form in the six methods
/ / 2.1. Use the enableEvents JFrame and processWindowEvent
/ / Frame1.java

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_CLOSING) (
System.exit (0);
)
)
)

/ / 2.2. Interface directly to WindowListener
/ / Frame1.java

Import java.awt .*;
Import java.awt.event .*;

Public class Frame1 extends Frame implements 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 windowDeactivated (WindowEvent windowEvent) ()
)

/ / 2.3. Directly inherited 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 windowClosing (WindowEvent windowEvent) (
System.exit (0);
)
)

/ / 2.4. Indirectly inherited form adapter WindowAdapter
/ / 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 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. Indirectly achieve 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 ();
)
)

No comments: