Java Foundation 8:gui

Source: Internet
Author: User
Tags event listener


On the Java foundation of the article, I think it can also be written in my other blog, is definitely original, and now share to everyone out.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ---------


I. Overview


GUI full name graphical user Interfaces, translated into GUI, also known as graphical user interface, GUI refers to the computer operation user interface which is displayed graphically.


But Java in the GUI does not have much advantage, Java's advantage is still lies in the network, here as the understanding can, do not delve into.

There are two essential conditions for implementing a GUI: components and Events

Java encapsulates the interface's components into objects and puts them in two packages: java. AWT and Javax.swing.

------AWT Package: Abstract window Toolkit, which is a toolkit for abstraction windows. To invoke the Local System method implementation functionality, it is a weight-level control.

------Swing Package: A graphical interface system based on AWT that provides more components and is fully implemented in Java, which enhances portability and belongs to lightweight controls.


Second, the AWT package

\

Relationship of 1,container (container) and component (component)

The two core classes in AWT are container (containers) and component (components)


The most basic component of Java's graphical user interface is the Component,component class and its subclasses that describe GUI elements that graphically display on the screen and interact with the user, such as a button, a label, and so on.

The general Component object cannot be displayed independently, and must be "placed" in a container object to display it.


Container is a component subclass, container subclasses can "hold" other component objects, or they can be added to other component objects as container objects.

The container object can use the method Add () to add additional component objects to it.


If you don't understand it, it's okay, I'll say it simply

Whether a container (container) or a button or a label (label) is a component, the container can add additional components.


2,window and Pannel

A,window: A top-level window whose object represents a free berth

B,panel: Its object can be used as a host to other component objects, but cannot exist independently and must be added to other container (such as window or applet)

3,frame:

A frame is a subclass of window, and an object created by a frame or its subclasses is a form


4, for example:

<span style= "FONT-SIZE:18PX;" >frame f = new Frame ("Test form"), F.setlayout (New FlowLayout ()); F.setvisible (true); F.setbounds (600, 200, 500, 400); Button B = New button ("one button"); F.add (b);</span>
Third, layout manager


Java is run across platforms, but different platforms do not have exactly the same definitions for points and coordinates. And the resolution of the screen can also cause changes in location, in order to ensure the relative position and size of each component and appearance,Java design layout Manager.

1, Layout Manager Category:

FlowLayout: Floating layout manager

BorderLayout: Border layout manager

GridLayout: Grid layout Manager

CardLayout: Card layout Manager

GridBagLayout: Grid Package Layout Manager

If you have done Web development, you will feel that Java layout management is a bit like the layout of the Web page, in fact, the Web page is also on different platforms, different resolutions of the computer running, from this point and Java is really quite like.

2,FlowLayout

Is the default manager for the Panel container, whose components float in the container, and its placement pattern is from top to bottom, from left to right.

<span style= "FONT-SIZE:18PX;" >public class Frameflow {public static void main (string[] args) {//Create a framework frame F = new Frame ("Test");//Set Form size F.setsiz E (400, 300);//Set the form display position f.setlocation (+); F.setbackground (Color.orange); Form background Color f.setvisible (true); Form visible f.setlayout (new FlowLayout ());//Set Floating layout mode for (int i = 0; i < i++) {F.add (New button ("btn" + i);}}} </span>


3,borderlayout

Layout The container's border, you can arrange the container components and resize them to conform to the five areas: North, South, east, west, middle. Each zone can contain at most one component and is identified by the corresponding constants: North, South, EAST, WEST, center, if there is only one component, the default location is CENTER, which is the default Layout Manager for window, frame, and dialog.


<span style= "FONT-SIZE:18PX;" >public static void Main (String args[]) {          Frame F;          F=new Frame ("Border Layout");          Button Bn=new button ("North");          Button Bs=new button ("South");          Button Bw=new button ("West");          Button Be=new button ("East");          Button Bc=new button ("medium");          F.add (Bn,borderlayout.north);          F.add (Bs,borderlayout.south);          F.add (bw,borderlayout.west);          F.add (be,borderlayout.east);          F.add (bc,borderlayout.center);                    F.setsize (200,200);          F.setvisible (True);          }  </span>



Other kinds of layout implementation is similar, I do not write the code, you can try it yourself.


Iv. Event Monitoring Mechanism


The composition of the event monitoring mechanism:

Event: A user in an interface of an operation, usually using a variety of input devices, such as the mouse, keyboard and so on.

Event Source: The component that generated the event, such as clicking on a button is the event source

Listener: Contains the event handler, which is responsible for checking whether the event occurred and, if so, activating the event handler to process it

Event handling: How the event is handled after it is raised.




Example:

<span style= "FONT-SIZE:18PX;" >public static void Main (string[] args) {frame f = new Frame ("Test form"); F.setlayout (new FlowLayout ()); F.setvisible (True) ; F.setbounds (600, 200, 500, 400); Button B = New button ("one button"), F.add (b),/**************** add Event listener mechanism **************///Add Close button F.addwindowlistener (new Windowadapter () {///If you implement the WindowListener interface with subclasses, you need to overwrite 7 of them, only the closing action is used, and the other actions are not used, but all must be rewritten. 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. @Overridepublic void windowclosing (WindowEvent e) {system.exit (0);}}); /Add button click event B.addactionlistener (New ActionListener () {///because the Actionlisterer interface only implements a method, so no adapter is required, no **adapter class,// However, if the implementation method of more than 2 must have **adapterprivate int num  = 0; @Overridepublic void actionperformed (ActionEvent e) {num++; System.out.println ("clicked button once" + num);}); </span>


Note: Here the event is added using anonymous inner class, if you are not clear about the anonymous inner class, please see my previous article

Http://blog.csdn.net/jin870132690/article/details/41597143#t3


V. Last


I'm not going to introduce you to swing's knowledge, you can implement visual programming with swing packages, and people who have had a. NET development experience should understand that dragging and dropping the interface is very convenient and interesting to try.

Interface is for users to see, in fact, the user is concerned about a large part of the interface is not beautiful, want to do a well received by everyone's software, regardless of the interface is necessary to beautify.

,


Java Foundation 8:gui

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.