1. In order to support graphical user interface programming, java1.0 's standard class library contains an abstract window toolbox (TOOLKIT,AWT).
The toolkit is extremely immature and its programming model is not object-oriented and has great limitations. To do this, java1.2 finally decided to abandon the GUI in AWT, instead adding a swing library.
Swing components can not only completely replace AWT components, but also provide more complex components.
It is important to note that GUI programming is that you cannot mix AWT components with swing components (for example, you cannot add javax.swing.JList to Java.awt.Frame). The class name of the swing component used begins with the letter J to differentiate the AWT components.
Note: Swing is based on the AWT architecture and does not completely replace AWT.
To create a frame:
1 ImportJava.awt.Container;2 3 ImportJavax.swing.JButton;4 ImportJavax.swing.JFrame;5 6 Public classFRAMEDMO {7 Public Static voidMain (string[] args) {8JFrame frame =NewJFrame ("Test JFrame"); 9 frame.setdefaultcloseoperation (jframe.exit_on_close);//end run when frame is closedTenFrame.setsize (500, 300);//Set Frame size OneFrame.setvisible (true);//Display frame A } -}
Setdefaultcloseoperation () tells the program what to do when it closes the current frame. Jframe.exit_on_close is to tell the program to close the program to end the run. Otherwise, after the framework is closed, the program does not end.
Once the framework is created, you can add components
1 ImportJava.awt.Container;2 3 ImportJavax.swing.JButton;4 ImportJavax.swing.JFrame;5 6 Public classFRAMEDMO {7 Public Static voidMain (string[] args) {8JFrame frame =NewJFrame ("Test JFrame");9JButton button=NewJButton ("button");Ten //Gets the contents pane of the frame OneContainer container=Frame.getcontentpane (); A //Add a frame to the contents pane of a frame - container.add (button); - //End run when frame is closed the frame.setdefaultcloseoperation (jframe.exit_on_close); - //Set Frame size -Frame.setsize (500, 300); - //Show Frame +Frame.setvisible (true); - } +}
Before Java SE5, if you want to add a component to a frame, you must first call the Getcontentpane in the class JFrame to get the contents pane of the frame,
The contents pane is then called by the method add to add the component to the Contents pane. For example, the example above.
The contents pane delegate.
1 ImportJavax.swing.JButton;2 ImportJavax.swing.JFrame;3 4 Public classFrameWithButton1extendsJFrame {5 PublicFrameWithButton1 () {6JButton button=NewJButton ("button");7Add (button);//JavaSE5 followed by8 }9 Public Static voidMain (string[] args) {TenFrameWithButton1 frame=NewFrameWithButton1 (); OneFrame.settitle ("Add a button to the frame"); A frame.setdefaultcloseoperation (jframe.exit_on_close); -Frame.setsize (500,300); -Frame.setvisible (true); the } -}
The container that needs to add components to its contents pane has Jframe,jdialog,japplet,jinternalframe,jwindow.
Basic graphics for the Java GUI