The base of GUI design applied in Java programming _java

Source: Internet
Author: User

Earlier procedures used the simplest input and output methods, the user input data in the keyboard, the program will output information on the screen. Modern procedures require the use of graphical user interface (graphical user Interface,gui), the interface has a menu, buttons, etc., the user through the mouse to select the menu options and click the button, command program function module. This chapter learns how to write GUI scientific experiments in the Java language and how to implement input and output through the GUI.
AWT and Swing

Previously written GUI programs in Java, using the Abstract Window Toolkit AWT (abstract Windows Toolkit). Now use swing more. Swing can be viewed as an improved version of AWT, rather than as an alternative to AWT, and is an improvement and extension of AWT. Therefore, both swing and AWT are useful when writing GUI programs. They coexist in the Java base class (Java Foundation CLASS,JFC).

Although both AWT and swing provide classes that construct graphical interface elements, they differ in their important aspects: AWT relies on the main platform to draw user interface components, and swing has its own mechanism to draw and manage interfaces in the windows provided by the main platform. The most obvious difference between swing and AWT is the appearance of the interface component, where AWT runs the same program on different platforms, and the appearance and style of the interface may differ. However, a swing based application might have the same appearance and style on any platform.

Classes in swing are inherited from AWT, and some swing classes directly extend the corresponding classes in AWT. For example, JApplet, JDialog, JFrame, and JWindow.

Using swing to design the graphical interface, mainly introduces two packages:

    1. Javax.swing Package: Basic classes that contain swing;
    2. Java.awt.event Package: Contains interfaces and classes related to handling events.

Because swing is too rich to give a full introduction to swing in a textbook, the knowledge of swing in this chapter is enough to allow readers to write fairly elegant GUI programs.
Components and Containers

A component (component) is the basic element of a graphical interface, and can be manipulated directly by the user, such as a button. A container (Container) is a composite element of a graphical interface that can contain components, such as panels.

The Java language is predefined for each component, and the program passes them or their subclasses to various component objects, such as the predefined button class JButton in Swing is a class, the JButton object that the program creates, or the object of the JButton subclass is the button. The Java language also has predefined classes for each container, and the program creates various container objects through them or their subclasses. For example, a predefined window class JFrame in Swing is a container class, and the object of the JFrame or JFrame subclass created by the program is the window.

To unify management components and containers, define superclass for all component classes, and define common operations for components in the component class. Similarly, the Superclass container class is defined for all container classes, and the common operation of the container is defined in the container class. For example, the Add () method is defined in the container class, and most containers can add components to the container with the Add () method.

The Component, container, and graphics classes are key classes in the AWT library. To construct a complex graphical interface hierarchically, the container is treated as a special component that can be put into another container. For example, put several buttons and text boxes in two panels, and then put the two panels and other buttons into the window. This hierarchical approach to constructing interfaces enables the construction of complex user interfaces in an incremental manner.
Fundamentals of event-driven programming

1. Event, monitor and monitor registration
Events on the graphical interface refer to user actions occurring on a component. For example, when a user clicks a button on the interface, it says that an event occurred on the button, which is the trigger for the event. The object that monitors the event is called a monitor, and the monitor provides a way to respond to events. In order for the monitor to be associated with the event object, you need to register the event object as a monitor for the system event object.

In the case of the Program Response button event, the program creates the button object, adds it to the interface, registers the button for the monitor, and the program has a way of responding to the button event. When the Click Button event occurs, the system invokes the event-handling method that has been registered for this button, completing the task of handling the button event.

2. The way to achieve event handling
There are two main scenarios for writing event handlers in the Java language: one is the program reset Handleevent (EVENTEVT), and the program takes a little bit more work, and the other program implements some system-set interfaces. Java provides a variety of interfaces by event type, and the class that acts as a monitor object needs to implement the appropriate interface, that is, the method that implements the response event. When an event occurs, the Handleevent (event evt) method inside the system automatically invokes the method of responding to events implemented by the Monitor's class.

The model used to detect and react to events in the Java.awt.event package consists of the following three constituent elements:
Source object: The event "occurs" on this component, it is in contact with a set of objects that "listen" to the event.
Monitor object: An object of a class that implements a predefined interface that provides a way to handle the event that occurs.
Event object: It contains information that describes a specific event that was passed from the source to the monitor when the event occurred.

The work that an event driver does in addition to creating source and monitor objects, you must also schedule the monitor to understand the source object or register the monitor with the source object. Each source object has a list of registered monitors that provide a way to add a monitor object to the list. The system will notify the monitor object of events that occur on the source object only after the source object has registered a monitor.

3. Event Type and monitor interface
In the Java language, events are categorized, called event types, to facilitate system-managed events and to facilitate program-registered monitors. The system provides an interface for each event type. The class that is to be the monitor object must implement the appropriate interface, providing a method of responding to the event as specified by the interface.

In the case of the Program Response button event, the JButton Class object button can be the trigger of an event. The button object produces a ActionEvent type event when the user clicks the buttons in the interface corresponding to a button. If the monitor object is obj, and the class of object obj is obj, class obj must implement the ActionListener interface in AWT to implement the Actionperformed method for monitoring button events. The button object must register its monitor obj with the addActionListener method.

When the program is running, when the user clicks the button object, the system passes a ActionEvent object to the monitor from the event firing object. The ActionEvent object contains information about which button the event occurred and other information about the event.

The following table gives some typical event types and some swing components that generate these events. When the actual event occurs, a series of events usually occur, for example, when the user clicks the button, the ChangeEvent event prompts the cursor to the button, and then a ChangeEvent event indicates that the mouse is pressed, and then the ActionEvent event indicates that the mouse has been released, But the cursor is still on the button, and finally the ChangeEvent event, which means the cursor has left the button. However, an application typically handles only a single ActionEvent event that presses the full action of a button.

Each event type has a corresponding monitor interface, and table 11-2 lists the methods for each interface. The class that implements the monitor interface must implement all the methods defined in the 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.