Previously, the program used the simplest input and output method. The user inputted data on the keyboard, and the program outputted information on the screen. Modern programs require the use of graphical user interface (GUI), which includes menus, buttons, etc. The user selects options in the menu and clicks buttons through the mouse to command the program function module. This chapter learns how to write GUI scientific experiments in Java language and how to implement input and output through GUI.
AWT and Swing
Previously, I wrote GUI programs in Java, using the abstract window toolkit AWT (Abstract Window Toolkit). Now I use Swing more frequently. Swing can be regarded as an improved version of AWT, rather than replacing AWT, and is an improvement and extension of AWT. Therefore, when writing GUI programs, both Swing and AWT must be used. They coexist in Java Foundation Class (JFC).
Although both AWT and Swing provide classes that construct graphical interface elements, their important aspects vary: AWT relies on the main platform to draw user interface components; while Swing has its own mechanism to draw and manage in windows provided by the main platform. Interface components. The most obvious difference between Swing and AWT is the appearance of the interface components. AWT runs the same program on different platforms, and the appearance and style of the interface may vary a few times. However, a Swing-based application may have the same look 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 a graphical interface, two main packages are introduced:
Because Swing is too rich, it is impossible to give a comprehensive introduction to Swing in a textbook, but the knowledge about Swing introduced in this chapter is enough to allow readers to write quite exquisite GUI programs.
Components and containers
Components are basic elements of graphical interfaces that users can operate directly, such as buttons. A container is a composite element of a graphical interface, and the container may contain components, such as a panel.
Java language predefined classes for each component, and programs pass various component objects through them or their subclasses. For example, the predefined button class JButton in Swing is a class, a JButton object created by the program, or an object of the JButton subclass. It's the button. The Java language also predefined classes for each container, and programs create various container objects through them or their subclasses. For example, the predefined window class JFrame in Swing is a container class, and the object of the JFrame or JFrame subclass created by the program is a window.
In order to manage components and containers in a unified manner, superclasses are defined for all component classes, and the components' common operations are defined in the Component class. Similarly, define the superclass Container class for all container classes, and define the container's shared operations 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.
Component, Container, and Graphics classes are key classes in the AWT library. To construct complex graphical interfaces at a hierarchical level, containers are treated as special components that can be placed in another container. For example, separate several buttons and text boxes in two panels, and then place these two panels and other buttons into the window. This method of hierarchically constructing interfaces can construct complex user interfaces in incremental ways.
Basics of Event Driver Design
1. Events, monitors and monitor registration <br /> Events on the graphical interface refer to user operations occurring on a component. For example, if a user clicks a button on the interface, it means that an event has occurred on this button, and the button object is the trigger of the event. The object that monitors events is called a monitor, and the monitor provides a method to handle events. In order to associate the monitor with the event object, it is necessary to register the event object to tell the system the monitor of the event object.
Taking the program responds to button events as an example, the program wants to create a button object, add it to the interface, register the button as a monitor, and the program needs to have a method to respond to button events. When the "click button" event occurs, the system calls the event processing method registered for this button to complete the work of handling button events.
2. Realize the mission of event handling
There are two main solutions for writing event handlers in Java language: one is to reset the handleEvent(Eventevt), and the program using this solution is slightly more workload; the other solution is to implement some system-set interfaces. Java provides multiple interfaces according to event type. As a class of monitor object, it needs to implement corresponding interfaces, that is, implement methods to respond to events. When an event occurs, the system's built-in handleEvent(Event evt) method automatically calls the method to respond to events implemented by the monitor's class.
The model used in the java.awt.event package to detect and react to events includes the following three components:
Source object: On the component of the event "occurres" it maintains contact with a group of objects that "listen" to the event.
Monitor object: an object of a class that implements a predefined interface. The class of this object provides a method to handle the occurrence of events.
Event object: It contains information describing a specific event passed from the source to the monitor when an event occurs.
The work a event driver wants to do in addition to creating source objects and monitor objects must also arrange for the monitor to understand the source objects, or register the monitor with the source objects. Each source object has a registered monitor list, providing a way to add monitor objects to that list. The system will notify the monitor object of events that occur on the source object only after the source object has registered the monitor.
3. Event type and monitor interface <br />In the java language, in order to facilitate the system to manage events and to facilitate the program to register monitors, the system classifies events, which is called event types. The system provides an interface for each event type. The class to be a monitor object must implement the corresponding interface and provide the method of responding to events specified by the interface.
Taking the program response button event as an example, the JButton class object button can be the initiator of an event. When the user clicks the button corresponding to the button in the interface, the button object will generate an event of ActionEvent type. If the monitor object is obj and the class obj is Obj, then the class Obj must implement the ActionListener interface in AWT to implement the actionPerformed method of monitoring button events. The button object must register its monitor obj using the addActionListener method.
When the program is running, when the user clicks the button corresponding to the button object, the system passes an ActionEvent object from the event fire object to the monitor. The ActionEvent object contains information including which button the event occurs, and other information about the event.
The following table shows some representative event types and some Swing components that produce these events. When an actual event occurs, a series of events will usually occur. For example, if a user clicks a button, a ChangeEvent event will prompt the cursor to be on the button. Then another ChangeEvent event means that the mouse is pressed, and then an ActionEvent event means that the mouse has been released. , but the cursor is still on the button, and finally the ChangeEvent event indicates that the cursor has left the button. But applications usually only handle a single ActionEvent event of the full action of pressing a button.
Each event type has a corresponding monitor interface, and Table 11-2 lists the methods for each interface. Classes that implement monitor interfaces must implement all methods defined in the interface.