Dark Horse programmer------Java GUI (graphical user interface) Learning Summary

Source: Internet
Author: User

Java training, Android training, iOS training,. NET training </a>, look forward to communicating with you!

Gui:

  Graphical user Interface (GUI).

 That is, graphically, to display the interface of computer operation, so as to make it easier for users to operate more intuitively.

The objects provided for the GUI in Java are in the java.awt and javax.swing two packages.

Java. Awt:

Abstract window ToolKit (Abstraction Windows Toolkit).

You need to invoke the Local System method implementation function, which belongs to the heavyweight control.

Javax. Swing:

On the basis of AWT, a set of graphical interface system

It provides more components and is fully implemented by Java. Enhanced portability, a lightweight control.

Inheritance relationships in java.awt:

  

  Where container is a special component, for a container, you can add other components to come in.

Layout Manager:

  The layout of the components in the container is the way they are emitted.

Common layout managers:

      · FlowLayout(streaming layout manager)

        · From doing the right order

Default layout manager in panel

      · BorderLayout(Boundary layout manager)

        · East, West, south, north, middle

        · Frame Default layout Manager

      · GridLayout(Grid layout manager)

        · Matrix of rules

      · CardLayout(Card layout manager)

        · Options tab

      · GridBagLayout(Grid Package layout manager)

        · Non-rule matrices

Create a simple form

  Common subcategories of container: Window, Panel (panels, cannot exist separately)

Common sub-categories for window: Frame,dialog

To create a simple form:

/*
To create a graphical interface:
1. Create a frame form.
2. Basic settings for the form.
such as size, location, layout.
3. Define the components.
4. Add the component to the form by using the form's Add method.
5. Make the form appear.
*/
Class awtdemo{ publicstaticvoid main (string[] args) { New Frame ("My AWT"); F.setsize (500,400); F.setlocation (300,200); F.setlayout (new FlowLayout ());

Button B = New button ("I am a button");
F.add (b);

        f.setvisible (true);     }}

Time Monitoring Mechanism composition

1. Event Source (component)

2, Events (event)

3. Monitor (Liistener)

4. Event handling (handling after event initiation)

Event Monitoring Mechanism Flowchart:

Event Source: These are the graphical interface components in the AWT package or swing package.

Event: Each event source has its own unique corresponding event and common events.

Listener: The action that can trigger an event (more than one action) is already encapsulated in the listener. All three of them are already defined in Java. Get the object directly to use it.

What we have to do is to deal with the resulting action.

code example:

  Let the button have the function of exiting the program/       *        button is the event source.        so which listener do you choose?        by closing the form example, you know that you want to know which component has a unique listener.        you need to see the functionality of the Component object.         by reviewing the description of the button. The Discovery button supports a unique listening addactionlistener.      */

class Framedemo{

 
 // defines a reference to the component that is required in the drawing.
    PrivateFrame F; PrivateButton but;Framedemo () {init (); }     Public voidinit () {f=NewFRAME ("My Frame"); //basic settings for frame. F.setbounds (300,100,600,500); F.setlayout (NewFlowLayout ()); but=Newbutton ("My button"); //add a component to a frameF.add (But); //load the events on the form. MyEvent (); //display the form;F.setvisible (true); }    Private voidMyEvent () {F.addwindowlistener (NewWindowadapter () { Public voidwindowclosing (windowevent e) {system.exit (0); }        });But.addactionlistener (NewActionListener () {Private intCount = 1;  Public voidactionperformed (ActionEvent e) {system.exit (0);            }        }); }     Public Static voidMain (string[] args) {NewFramedemo (); }}

Menu

 The inheritance relationship of the menu:

   

Menu creation Process:

 1. First create the menu bar, then create the menu, and then set up the menu item in each menu.

2. The menu can be added to the menu as a submenu of the menu.

3. Add the menu bar to the frame through the Setmenubar () method.

A simple Notepad program code:

 PackageMyMenu;Importjava.awt.*;Importjava.awt.event.*;ImportJava.io.*; Public classmymenutest{PrivateFrame F; PrivateMenuBar Bar; PrivateTextArea ta; PrivateMenu Filemenu; PrivateMenuItem Openitem,saveitem,closeitem; PrivateFileDialog Opendia,savedia; Privatefile file;    Mymenutest () {init (); }

  //Form initialization method Public voidinit () {f=NewFrame ("My Window"); F.setbounds (300,100,650,600); Bar=NewMenuBar (); Ta=NewTextArea (); Filemenu=NewMenu ("File"); OpenItem=NewMenuItem ("Open"); SaveItem=NewMenuItem ("Save"); Closeitem=NewMenuItem ("Exit"); Filemenu.add (OpenItem); Filemenu.add (SaveItem); Filemenu.add (Closeitem); Bar.add (Filemenu); F.setmenubar (bar); Opendia=NewFileDialog (F, "I want to open", Filedialog.load); Savedia=NewFileDialog (F, "I want to save", Filedialog.save); F.add (TA); MyEvent (); F.setvisible (true); }

  //Event Listener processing method Private voidMyEvent () {
    //Save button Event Listener processing Saveitem.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {if(file==NULL) {savedia.setvisible (true); String Dirpath=savedia.getdirectory (); String FileName=Savedia.getfile (); if(dirpath==NULL|| filename==NULL) return ; File=NewFile (dirpath,filename); } Try{bufferedwriter BUFW=NewBufferedWriter (NewFileWriter (file)); String text=Ta.gettext (); Bufw.write (text); //Bufw.flush ();Bufw.close (); } Catch(IOException ex) {Throw Newruntimeexception ();    } } }); //Open button Event Listener processing Openitem.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {opendia.setvisible (true); String Dirpath=opendia.getdirectory (); String FileName=opendia.getfile ();//System.out.println (dirpath+ "..." +filename); if(dirpath==NULL|| filename==NULL) return ; Ta.settext (""); File=NewFile (dirpath,filename); Try{BufferedReader Bufr=NewBufferedReader (Newfilereader (file)); String Line=NULL; while((Line=bufr.readline ())! =NULL) {ta.append ( line+ "\ r \ n"); } bufr.close (); } Catch(IOException ex) {Throw NewRuntimeException ("Read failed"); } } });
   //Eject button for monitoring processing Closeitem.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {system.exit (0); } });
    //The form itself listens for processing F.addwindowlistener (NewWindowadapter () { Public voidwindowclosing (windowevent e) {system.exit (0); } }); } Public Static voidMain (string[] args) {Newmymenutest (); }}

Summarize:

Event Monitoring mechanism:

 1. Determine the event source (container or component)

2. The listener is registered to the event source through the Addxxxlistener () method of the event source object.

3, the method receives the Xxxlistener object, or the Xxxlistener subclass Xxxadapter object.

4, generally with anonymous internal class to represent.

5. When the method is overwritten, the parameter of the method is generally the xxxevent type of the variable to receive.

6, the general Xxxlistener method of more than 3 or 3 will certainly have a xxxadapter subclass object. (also known as adapters)

Dark Horse programmer------Java GUI (graphical user interface) Learning Summary

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.