Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!
The GUI (graphical user Interface) provides a graphical interface that allows users to interact with the system graphically. Before the GUI is popularized, the user usually has to control the computer in the form of a text command. The GUI intuitively presents the function of the computer to the user, reducing the user's threshold for using the computer. Apple and Microsoft are the pioneers of the GUI (though they have copied Xerox to some extent), and the GUI has brought a huge market return for the two-bit PC kings.
Early Mac GUI
The GUI requires operating system and hardware support. Therefore, GUI programming often has to deal with the problem of portability. Java GUI programming has relatively good portability. However, as the GUI's center of gravity shifts to the mobile end, the GUI part of Java is somewhat awkward. In any case, we can still learn some basic aspects of GUI programming through Java.
The understanding of graphics
Look at one of the following pictures:
Kturtle Drawing. See, make your child into a code farmer.
As you can see, there is a house with windows and doors on it, stripes on the windows, handles on the door, and a little turtle outside the image. We refer to houses, windows, doors, stripes, handles, all of which can be called objects. Different objects have a combination of (composition) relationships, such as windows and doors belonging to the house, while the handle belongs to the door. The turtle and the house are two objects independent of one another. In addition, there is a box outside the entire image to indicate the scope of the drawing, and all the above mentioned elements are attached to the box.
On the other hand, the above objects have many re-used graphical elements (component). For example, the handle is a circle, and the house and door are made of straight lines. The same graphic elements can be categorized as a class. We can reuse straight line classes to generate straight lines (of different natures) and combine them into different objects.
This is an object-oriented approach to understanding a graph. An object is a natural way to describe a graphic. Object-oriented programming has been a very successful application in computer graphics.
A simple GUI
Java's GUI functionality is mainly concentrated in the AWT and swing two packages. AWT is the GUI bottom package. Swing packages are high-rise packages that are easier to port. This will focus more on swing packages.
Import Javax.swing.*;import java.awt.*;p ublic class Helloworldswing { private static void Createandshowgui () { JFrame frame = new JFrame ("HelloWorld"); Frame.setdefaultcloseoperation (jframe.exit_on_close); Pane ' s layout Container cp = Frame.getcontentpane (); Cp.setlayout (New FlowLayout ()); Create button JButton B1 = new JButton ("click Me"); JButton b2 = new JButton ("shit"); Add Buttons Cp.add (B1); Cp.add (B2); Show the Window frame.pack (); Frame.setvisible (True); } public static void Main (string[] args) { Runnable tr = new Runnable () {public void run () { CREATEANDSHOWGU I (); } }; Javax.swing.SwingUtilities.invokeLater (TR); }}
In the main () method of the above program, we use the anonymous class (anonymous Class) to define the thread runnable tr. An anonymous class is a nested class of Java that uses a {} to directly include the definition of a class when creating an object using new. In the anonymous class definition, we do not need to describe the class name. After new follows the interface () or class (), the definition of the anonymous class will implement the interface or inherit the class.
The results of the operation are as follows:
Graphics tree
We use the Add () method to add a graphic element to another element. With such a combination, all the graphical elements form a tree-like data structure that represents the affiliation between the image elements (containment hierarchy). A graphical tree represents a GUI graphical interface.
Graphics tree
In the program, we first created the JFrame object. JFrame is top-level container, the root of the graph tree. JFrame contains content Pane by default. Content pane is a container object that typically contains all the visible elements of a graphic (except for the menu menubar). The Content pane contains two buttons, the JButton element.
The SetLayout () method of the Content pane determines how the elements are laid out (layout). The layout determines the position of the element. The most straightforward layout is the direct description of the element's coordinate position (in pixels). However, the size of the GUI may vary greatly, the hard-to-specify pixel location will significantly reduce the portability of the program. Swing provides some of the higher-level layout methods, such as FlowLayout, where elements are left-to-right, and then go to the next row after they are full.
More ways to lay out Java
Graphic elements
In addition to the buttons, we can add more elements to the GUI, most of which are derived classes of jcomponent. Like what:
Import Javax.swing.*;import java.awt.*;p ublic class Helloworldswing {private static void Createandshowgui () {J Frame frame = new JFrame ("HelloWorld"); Frame.setdefaultcloseoperation (Jframe.exit_on_close); Pane ' s layout Container CP = Frame.getcontentpane (); Cp.setlayout (New GridLayout (0,2)); JButton JButton button = new JButton ("click Me"); JLabel label = new JLabel ("OK"); JPanel JPanel Panel1 = new JPanel (new BorderLayout ()); JPanel Panel2 = new JPanel (new BorderLayout ()); Panel2.setbackground (color.red); Panel1.add (button, borderlayout.center); Cp.add (PANEL1); Panel2.add (label, Borderlayout.east); Cp.add (PANEL2); JList string[] lines = {"A", "B", "C"}; JList list = new JList (lines); Cp.add (list); Jcheckbox Cp.add (New Jcheckbox ());
Show the Window Frame.pack (); Frame.setvisible (TRUE); public static void Main (string[] args) {Runnable tr = new Runnable () {public void run () { Createandshowgui (); } }; Javax.swing.SwingUtilities.invokeLater (TR); }}
The GridLayout is used here, with the following effects:
JComponent
More elements
Summarize
Here are just a few examples of GUI programming to get a conceptual understanding of GUI programming. With the depth of use, we are likely to move into the IDE design GUI and generate GUI code automatically. In any case, the concept of understanding is essential.
The knowledge of the GUI helps to learn the mobile side development.
Java Vamei Quick Tutorial GUI