Java Basic Learning--26, GUI

Source: Internet
Author: User
Tags class definition

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 one 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.*;PublicClasshelloworldswing {PrivateStaticvoidCreateandshowgui () {JFrame frame =New JFrame ("HelloWorld"); Frame.setdefaultcloseoperation (Jframe.exit_on_close);//Pane ' s layout Container CP =Frame.getcontentpane (); Cp.setlayout (NewFlowLayout ());//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 (truepublic static void main (string[] args) {Runnable tr = new  Runnable () {public void< Span style= "COLOR: #000000" > run () {Createandshowgui ();}}; 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.*;PublicClasshelloworldswing {PrivateStaticvoidCreateandshowgui () {JFrame 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 (NewBorderLayout ()); JPanel Panel2 =New JPanel (NewBorderLayout ()); 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< Span style= "COLOR: #000000" > Jcheckbox ()); 
// show the Window Frame.pack (); Frame.setvisible (truepublic static void main (string[] args) {Runnable tr = new Runnable () {public void< Span style= "COLOR: #000000" > run () {Createandshowgui ();}}; Javax.swing.SwingUtilities.invokeLater (TR); }}

The GridLayout is used here, with the following effects:

JComponent

Java Basic Learning--26, GUI

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.