Dark Horse Programmer-"Java Basics"--gui (graphical user interface)

Source: Internet
Author: User

I. Overview

1, GUI (Graphicaluser Interface): Also known as graphical user interface, is a computer user and computer interaction of a way.

2, the user interacts with the computer in two ways: GUI and CLI.

(1) GUI (graphical user Interface): Graphical user interface, graphically, to display the computer operating interface, convenient and intuitive.

(2) CLI (Command Lineuser Interface): Commands line user interface, that is, the Common DOS command line operation, must remember some commands, the operation is not intuitive.

3. The GUI objects provided by Java are found in the java.awt and javax.swing two packages, where:

(1) java. AWT (Abstract window Toolkit): The Abstraction Windows Toolkit, called the Local System method implementation function, is a heavyweight control.

(2) javax. Swing Package: A set of graphical interface systems built on the basis of AWT, with more components and implemented entirely in Java, is a lightweight control.

Second, the inheritance Relationship diagram

  

Note: Container is a container-type component in which additional components can be added through the Add method. Container common subclasses: Window, Panel (cannot exist alone).

Third, layout manager

1. Layout: How the components are arranged in the container.

2. Common layout Manager:

FlowLayout (Streaming layout manager)

| The arrangement in the same way is the default layout management of frame.

GridLayout (web format layout manager)

| is a tab.

Gridbaylayout: (Grid package layout manager)

|---non-regular matrices

Four, Frame Demo

(a) Frame Create steps

1. Create a Frame form:

Frame f = new Frame ("my frame");//You can set the title, that is, the form name

2, basic settings for the form: such as size, location, layout, etc.:

f.setsize (int wight,int hight);//Form Size setting

f.setlocation (int x,int y);//form display position setting, horizontal ordinate

F.setbounds (int x,int y,int wight,int hight), you can also use this method to set the size and position

F.setlayout (layout layout), parameters for the specified layouts manager, such as FlowLayout

3. Define the components:

Button B = New button ("my button");//can set the name of the component

4. Add the component to the form via the Add method:

F.add (b);//Add a button component into the form

5. Let the form display:

F.setvisible (Boolean B);//Whether the form is displayed by setting the parameter to TRUE or False

(ii) Frame Demo example

1 classframedemo{2      Public Static voidMain (string[] args) {3Frame f =NewFRAME ("My Frame");4 //f.setsize (500, 400); 5 //f.setlocation (+);6F.setbounds (400, 200, 500, 400);7F.setlayout (NewFlowLayout ());//set up a streaming layout8Button but =NewButton ("One buttons");9F.add (But);//Add a button to the form. TenF.setvisible (true); One     } A}

V. Event Monitoring Mechanism

(i) The incident monitoring mechanism consists of

(1) Event source (component).

(2) event.

(3) Listener (Listener).

(4) Event handling (how it is handled after the event is raised).

(ii) event monitoring mechanism flowchart

   

(iii) Steps for incident handling

(1) Determine the event source (container or component). The Listener (Listener) is registered to the event source through the Addxxxlistener () method of the event source object. The method receives the subclass object of the Xxxlistener, or the subclass object of the Xxxlistener subclass Xxxadapter.

(2) The anonymous inner class is generally used to represent. When overriding a method, the parameter of the method is generally the xxxevent type of the variable to receive.

1Frame f=NewFrame ("Demo Mouse and Keyboard Listener");2F.addwindowlistener (NewWindowadapter ()3 {4 @Override5       Public voidwindowclosing (windowevent e)6      {7System.exit (0);//indicates close window8      }9});

Description

(1) When an event is triggered, the event is packaged into a variable that the object passes to the parameter in the Overwrite method. (which includes the event source object, obtained through GetSource () or getcomponent (). )

(2) If the sub-class implementation of the WindowListener interface, you need to cover 7 of the methods, you can only use the closing action, other actions are not used, but must rewrite all. Because Windowlister's subclass Windowadapter (adapter) already implements this interface, it overrides all of the methods. Then simply inherit the Windowadapter and overwrite the required methods.

(3) Clear the event and deal with the event, in fact, add what listener will need to add what event.

(iv) Application examples

1./* keyboard events and mouse events */

1 ImportJava.awt.Button;2 Importjava.awt.FlowLayout;3 ImportJava.awt.Frame;4 ImportJava.awt.TextField;5 ImportJava.awt.event.KeyAdapter;6 Importjava.awt.event.KeyEvent;7 ImportJava.awt.event.MouseAdapter;8 Importjava.awt.event.MouseEvent;9 ImportJava.awt.event.WindowAdapter;Ten Importjava.awt.event.WindowEvent; One  Public classMouseandkeydemo { A     PrivateFrame F; -     PrivateTextField TF; -     PrivateButton but; the //constructor, which is used to initialize the -      PublicMouseandkeydemo () { - init (); -     } +     //form creation and feature implementation -     Private voidinit () { +f =NewFrame ("Mouse and Keyboard listener demo"); AF.setbounds (400, 200, 500, 400); atF.setlayout (NewFlowLayout ()); -  -tf =NewTextField (35); -but =NewButton ("One buttons"); -  - F.add (TF); in F.add (But); -  to MyEvent (); +  -F.setvisible (true); the     } *     //Registering Events $     Private voidMyEvent () {Panax Notoginseng         //Add a keyboard listener to the text box.  -Tf.addkeylistener (NewKeyadapter () { the @Override +              Public voidkeypressed (KeyEvent e) { A                 if(E.iscontroldown () && e.getkeycode () = =keyevent.vk_enter) { theSystem.out.println ("Enter run ..."); +                 } -             } $         }); $  -F.addwindowlistener (NewWindowadapter () { - @Override the              Public voidwindowclosing (windowevent e) { -System.exit (0);Wuyi             } the         }); -  Wu         //add a mouse listener on the button. -But.addmouselistener (NewMouseadapter () { About             Private intCount = 1; $ @Override -              Public voidmouseclicked (MouseEvent e) { -                 if(E.getclickcount () = = 2) -Tf.settext ("Mouse double click ..." + count++); A             } +         }); the     } -      Public Static voidMain (string[] args) { $         NewMouseandkeydemo (); the     } the}

Dark Horse Programmer-"Java Basics"--gui (graphical user interface)

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.