Java basics-GUI programming (1), java basics gui Programming
I. Definition
The full name of GUI is Graphical User Interface, that is, Graphical User Interface. JDK provides two packages, AWT and Swing, for GUI program design and development.
1. java. awt abstract Window Toolkit (abstract Window Toolkit), which is an early version of java and has a limited variety of components. It needs to call local system methods to implement functions. It is heavyweight and somewhat dependent on the platform.
2. javax. SWing is a set of graphical user interface systems built by sun on the basis of AWT. It provides more components and is fully implemented by java, enhancing portability and lightweight.
SWing is equivalent to an upgraded version of AWT, which solves the problem of cross-platform operations. However, it does not mean that AWT has been completely eliminated. It is the foundation and its performance is very important.
Ii. AWT class hierarchy
Container: A Container. It is a special component. You can add other components in this component through the add method.
Simple code example:
Import java. awt. frame; public class Test19 {public static void main (String [] args) {demo ();} static void demo () {Frame f = new Frame ("this is my first window"); f. setSize (400,400); f. setLocation (40, 60); // Coordinate System in the upper left corner. out. println ("succeeded"); // verify whether the program has been executed }}
If the above code is executed, the program has not been suspended and has been executed. The desired dialog box does not appear.
The above code is missing:F. setVisible (true );
Import java. awt. frame; public class Test19 {public static void main (String [] args) {demo ();} static void demo () {Frame f = new Frame ("this is my first window"); f. setSize (400,400); f. setLocation (40, 60); // coordinate f in the upper left corner. setVisible (true); // make the window visible to System. out. println ("succeeded"); // verify whether the program has been executed }}
Execution result: (it is worth noting that in this small window, clicking X in the upper right corner does not close the window. We need to set it here)
The following shows how to add a button:
Import java. awt. button; import java. awt. frame; public class Test19 {public static void main (String [] args) {demo ();} static void demo () {Frame f = new Frame ("this is my first window"); f. setSize (400,400); f. setLocation (40, 60); // coordinate f in the upper left corner. setVisible (true); // make the window visible. Button B = new Button ("Button"); // you need to export the package f. add (B); // add the button System. out. println ("succeeded"); // verify whether the program has been executed }}
Execution result: (Note: if there is a button, the button is full by default when the size and position are not set)
Summary:
Based on its role, many GUI components can be divided into two categories: basic components and containers.
Basic components: buttons, text boxes, and other components.
Container: it can accommodate other components, such as Windows and dialog boxes. All containers are directly or indirectly sub-classes of java. awt. Container (the Frame above is a Container)
Appendix, thoughts:
The above program is executed to System. out. println ("Running succeeded"); It is over. In fact, the main function is over. But the window is still in. We can even perform operations. There must be other front-end threads running. It can be understood that when a Frame is created, another thread is started.