[Java GUI] Java GUI basics and javagui Basics

Source: Internet
Author: User

[Java GUI] Java GUI basics and javagui Basics
AWT and Swing

Swing is an improvement and expansion of AWT. Swing and AWT both work when writing GUI programs. They coexist in Java basic classes (JFC.

Although both AWT and Swing provide classes for constructing graphical interface elements, they have different wormhole aspects. AWT relies on the main platform to draw user interface components, while Swing has its own mechanism to draw and manage interface components in the Windows provided by the main platform. The most obvious difference between Swing and AWT is the appearance of the interface components. When AWT runs the same program on different platforms, the appearance and style of the interface may be slightly different. However, a Swing-based application may have the same appearance and style on any platform.

Classes in Swing inherit from AWT, and some Swing classes directly extend the corresponding classes in AWT. For example, JApplet JDialog JFrame JWindow

Use Swing to design a graphical interface, mainly introduce two packages
Javax. swing package and java. awt. event package

Components and containers

Components are the basic elements of the graphic interface. You can directly operate them, such as buttons.
A container is a composite element of the graphic interface. A container can contain components.

The Java language predefines classes for each component. programs use them or their subclasses for various component objects. For example, the pre-defined button class JButton in Swing is a type, the JButton object created by the program, or the object of the JButton subclass is the button.
Java also predefines classes for each type of container. programs create various container objects through them or their subclasses. For example, the pre-defined window class JFrame in Swing is a container class, and the objects of the JFrame or JFrame subclass created by a program are windows.

To manage components and containers in a unified manner, define the superclass for All Component classes and define the common operations of components in the Component class. Similarly, define the super-class Container class for all Container classes and define the common operations of containers in the Container class. For example, the add () method is defined in the Container class. Most containers can use the add () method to add components to the Container.

The Component Container and Graphics classes are the key classes in the AWT library. In order to construct a complex graphic interface in layers, containers are treated as special components and can be placed into another container. This hierarchical interface construction method constructs complex user interfaces in incremental mode.

Basics of event Driver Design

1. event/monitor Registration
A Gui event refers to a user action on a component. For example, if the user runs a single button on the interface, it means that an event has occurred on the button, and the button object is the event producer. The object for event monitoring becomes a monitor, and the monitor provides a method to respond to the event. To associate the monitor with the event object, you must register the event object as a monitor to notify the System of the monitor of the event object.

Take the corresponding button event of the program as an example. The program must create a button object, add it to the interface, and register it for the button MONITOR. The program must have a method to respond to the button event. When a single button event occurs, the system calls the event processing method registered for this button to complete the processing of the button event.

2. How to Handle events
There are two main solutions for writing event handlers in Java. One is to reset handleEvent, and the workload of programs using this method is slightly larger. Another method is to implement some system-defined interfaces. Java provides multiple interfaces based on the event type. The Listener class of the monitor object implements the corresponding interface, that is, the method for responding to the event. When an event occurs, the handleEvent method in the system automatically calls the event response method implemented by the class of the monitor.

The model used to detect and respond to events in the java. awt. event package includes the following three elements:
① Source object
② Monitor object
③ Event object

In addition to creating source and monitor objects, an event driver must also arrange the monitor to understand the source object or register the monitor with the source object. Each source object has a registered monitor list, providing a way to add a monitor object to this list. The system notifies the monitor object of the time when a monitor occurs on the source object only after the monitor object is registered.

3. event types and monitor Interfaces
In Java, to facilitate system management and monitor registration, the system classifies time as an event type. The system provides an interface for each event type. The class to act as the monitor object must implement the corresponding interface and provide the response time method specified by the interface.

Take the corresponding button event of the program as an example. The JButton Class Object button can be an event producer. When you click the button corresponding to the button in the interface, the button object will generate an ActionEvent type time. If the obj class of the object obj is Obj, The Obj class must implement the addActionListener method to register its monitor obj.

When the program is running, When you click the button corresponding to the button object, the system will talk about an ActionEvent object from the event to the monitor. The information contained in the ActionEvent object includes the button where the event occurred and other information about the event.

When an actual event occurs, a series of time is usually generated. For example, when you click a button, a ChangeEvent event is generated, prompting you to cursor over the button. Then a ChangeEvent event indicates that the mouse is pressed, then the ActionEvent event indicates that the mouse has been released, but the cursor is still on the button, and the ChangeEvent event is final, indicating that the cursor has left the button, however, an application usually only processes a single ActionEvent event for the complete action of pressing the button.

Each event type has a corresponding Monitor interface. The class that implements the Monitor interface must implement all methods defined in the interface.

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.