Java-GUI Programming

Source: Internet
Author: User

GUI, Graphics User Interface, graphical User Interface. GUI intuitively presents the functions of the computer to the user, and the GUI is more intuitive and easy to accept.

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.

Three elements of GUI: components, containers, and events.

Components, Inherits certain functional modules, provides public interfaces for external calls, and has good reusability.

ContainerIn some languages, it is also called a form. A container is also a special component. You can place other components in the container and manage the positions of each component in the container through the layout manager.

EventEach component triggers action events. Different events in java are handled by different listeners. components are event sources, and some listeners are mainly used to listen to Action events generated from specified event sources.

I. Swing Components

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. The Swing package contains many Swing components.

The Swing component is independent of the local window system.

Besides the AbstractButton class, Swing components start with J.

Swing components are built based on AWT

Contains alternative components of the AWT visualization component and complex components-tree and table. Therefore, it provides a richer set of component types.



Ii. Container

A container is a special component. Other components can be placed in a container and controlled by the layout manager.

AWT provides two main container classes:


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + primary/2jwvcD4KPHA + primary/ydLU1/primary + primary/secondary + IDwvcD4KPHA + primary + PGJyPgo8L3A + primary/primary + primary = "http://www.2cto.com/uploadfile/Collfiles/20140312/20140312091637230.jpg" alt =" \ ">

FlowLayout is the simplest layout manager. The main idea is to arrange the components in the container by row first. After a row is arranged to the boundary, it is folded back to the next row to continue the arrangement, and so on. Panel uses this layout manager by default.

BorderLayout is the default layout manager of the Frame. It splits the form into five areas: North, South, East, West, and Center, and automatically adjusts the component size according to the size of the form. The default is the Center area.

GridLayout divides the space into regular rectangular grids, and each cell area is of the same size. The component is added to each cell. A line is first filled from left to right, followed by a line break, and then top to bottom.

The layout manager is responsible for the size and location of each component. In this case, you cannot set the size and location attributes of the component. If you want to set the layout manager by yourself, you can cancel the layout manager: setLayout (null)

Iii. Events

There are three types of events: Keyboard Events, mouse events, and Widget Action events.

Three objects are involved in event processing: Event source, event, and listener.

The event processing mechanism is a delegated event processing method: a common component delegates the entire event processing to a specific event listener. When the event SOURCE sends a specified event, the event listener is used to handle the event.

To implement the listening mechanism, you need to take the following three steps:

No.1In the declaration for the eventhandler class, one line of code specifies that the class either implements alistener interface or extends a class that implements a listener interface. Forexample:

Publicclass MyClass implement ActionListener{}


No. 2Another lineof code registers an instance of the event handler class as a listener on oneor more components. For example:

someComponent.addActionListener(instanceOfMyClass);


No. 3The event handler class has code that implementsthe methods in the listener interface. For example:

 public voidactionPerformed(ActionEvent e) {    ...//code that reacts to the action... }


The model is as follows:


Event listening can be implemented in four ways.: The class itself acts as the event listener; the external class acts as the event listening mechanism; the anonymous internal class acts as the event listener; and the internal class acts as the event listener.


Code:

Import java. awt. *; import java. awt. event. *; public class TestWindowClose {public static void main (String [] args) {new MyFrame55 ("MyFrame") ;}} class MyFrame55 extends Frame {MyFrame55 (String s) {super (s); setLayout (null); setBounds (300,300,400,300); this. setBackground (new Color (204,204,255); setVisible (true);/* this. addWindowListener (new MywindowMonitor (); * // * anonymous internal class. No one knows about the brackets. It is applicable to simple method logic and few statements, and infrequently changed Classes */this. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {setVisible (false); System. exit (-1) ;}}) ;}/ * internal class, only recognized by this class, out of this class, I don't know */class MywindowMonitor extends WindowAdapter {public void windowClosing (LOGIN wevent e) {setVisible (false); System. exit (0 );}}}


Summary

Whether a software is an excellent software makes the user interface design intuitive and important. When designing a GUI, developers need to fully communicate with software users, determine user needs and usage habits, and strike a balance between practicality and aesthetics.




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.