Java advanced 08 GUI build your child into a code farmer

Source: Internet
Author: User

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

Gui(Graphical user interface) providesGraphicalAllows users to interact with the system in a graphical manner. Prior to Gui promotion, users usually need to control computers by using text commands. Gui intuitively presents computer functions to users, reducing the threshold for users to use computers. Apple and Microsoft are pioneers in the GUI field (although both of them copied Xerox to a certain extent), and Gui has also brought lucrative market returns to the two PC Kings.

Early Mac Gui

The GUI must be supported by the operating system and hardware. Therefore, Gui programming often needs to handle portability issues. Java GUI programming has relatively good portability. However, as the focus of the GUI is shifted to the Mobile End, Some of Java's Gui status is awkward. In any case, we can still use Java to understand some basic content of GUI programming.

  Understanding of graphics

Take a look at the following picture:

Kturtle painting. Refer to building your child into a code farmer.

We can see that there is a house in the figure. There are windows and doors on the house, striped windows, and handles on the door. In addition, there is a small turtle outside the image. The house, windows, doors, stripes, and handles we mentioned can all be called objects. Different objects haveCombination(Composition) relationship, for example, the window and the door belong to the house, and the handle belongs to the door. Turtles and houses are mutually independent objects. In addition, there is a box outside the image to indicate the plotting range. All the elements mentioned above are attached to the box.

On the other hand, the above objects have many repeatedGraphicsElement(Component). For example, the handle is a circle, and the house and the door are composed of straight lines. The same graphic element can be regarded as oneClass(Class ). We can reuse the line class to generate (different properties) straight lines and combine them into different objects.

This is to understand a graph in an object-oriented way. An object is a natural way to describe a graph. Object-Oriented programming has been widely used in computer graphics.

 

A simple Gui

Java GUI functions are mainly concentrated in the AWT and swing packages. AWT is the underlying GUI package. The swing package is a high-level package, which is easier to transplant. Here we will focus more on Swing packages.

 Import Javax. Swing .* ;  Import Java. AWT .*;  Public   Class  Helloworldswing {  Private   Static   Void  Createandshowgui () {jframe Frame = New Jframe ("helloworld" ); Frame. setdefaclocloseoperation (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 () {createandshowgui () ;}; javax. Swing. swingutilities. invokelater (TR );}} 

AboveProgramIn the main () method, we useAnonymous class(Anonymous class) defines the thread runnable tr. An anonymous class is a nested class of Java.NewWhen creating an object{}To directly include the definition of a class. In the Anonymous class definition, we do not need to specify the class name. New followsInterface ()OrClass ()The anonymous class definition implements this interface or inherits this class.

 

The running result is as follows:

Graph tree

We useAdd ()Method to add a graphic element to another element. Through this combination, all the graphic elements constitute a tree-like data structure, which representsAffiliation(Containment hierarchy ). A graphic tree represents a GUI.

 

Graph tree

In the program, we first create a jframe object. Jframe is the top-level container, that is, the root of the graph tree. By default, jframe contains content pane. Content pane is a container object, which generally contains all visible elements of graphs (except menu menubar. Content pane contains two buttons, namely the jbutton element.

 

Content pane'sSetlayout ()The method determines the element'sLayout(Layout) method. The layout determines the position of the element. The most direct layout is to directly describe the Coordinate Position (pixel) of the element ). However, the size of the GUI device may vary greatly. The hardware requires that the pixel location greatly reduces the portability of the program. Swing provides higher-level layout methods. For example, in flowlayout, elements are arranged from left to right and enter the next row after the full layout.

More Java la s

 

Graphic elements

In addition to buttons, we can also add more elements to the GUI. These elements are mostly component classes of jcomponent. For example:

 Import Javax. Swing .* ;  Import Java. AWT .* ; Public   Class  Helloworldswing {  Private   Static   Void  Createandshowgui () {jframe Frame = New Jframe ("helloworld" ); Frame. setdefaclocloseoperation (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 );}}

 

Gridlayout is used here, and the effect is as follows:

Jcomponent

More Elements

 

Summary

Here is just a simple example of GUI programming, in order to understand the concept of GUI programming. With the in-depth use, we are likely to transfer to the IDE design GUI and automatically generate the GUICode. In any case, concepts are essential.

Gui knowledge helps you learn mobile development.

 

Welcome to continue reading the "Java quick tutorial" SeriesArticle

 

 

 

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.