Earlier programs used the simplest input and output methods,The user enters data on the keyboard,The program outputs the information on the screen.Modern programs require the use of a graphical user interface (graphical user interface, GUI). The interface has menus, buttons, and so on. The user selects options in the menu and clicks the button with the mouse.Command program function module.This chapter learns how to write gui scientific experiments in java language.How to implement input and output through gui.
awt and swing
Previously, the gui program was written in java using the abstract window toolkit (awt (abstract window toolkit)). Now more swing is used. swing can be seen as an improved version of awt,Instead of replacing awt, it is an improvement and extension of awt.Therefore, when writing a gui program, both swing and awt have an effect.They coexist in the java foundation class (jfc).
Although both awt and swing provide classes for constructing graphical interface elements,But their important aspects are different:awt relies on the main platform to draw user interface components;And swing has its own mechanism,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,awt runs the same program on different platforms,There may be some differences in the look and feel of the interface.However, a swing-based application may have the same look and feel on any platform.
The classes in swing are inherited from awt. Some swing classes directly extend the corresponding classes in awt.For example, japplet, jdialog, jframe, and jwindow.
Use swing to design a graphical interface,Two packages are mainly introduced:javax.swing package:contains the basic classes of swing; java.awt.event package:Contains interfaces and classes related to handling events.
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 for readers to write quite beautiful GUI programs.
Components and containers
A component is a basic element of a graphical interface.Users can operate directly,For example buttons.Containers are composite elements of the graphical interface.Containers can contain components,For example panels.
The java language has predefined classes for each component,Programs pass them or their subclasses to various component objects,For example, the predefined button class jbutton in swing is a class.The jbutton object created by the program, or an object of a subclass of jbutton is a button.The java language also predefined classes for each container,Programs create various container objects from them or their subclasses.For example, the predefined window class jframe in swing is a container class.The object of the jframe or jframe subclass created by the program is the window.
In order to uniformly manage components and containers,Define superclasses for all component classes,The common operations of the components are defined in the component class. Similarly, define the super class container class for all container classes, and define the common operations of the container in the container class. For example, the container class defines the add () method, and most containers can add components to the container using the add () method.
The component, container, and graphics classes are the key classes in the awt library.In order to be able to construct complex graphical interfaces hierarchically,Containers are treated as special components,You can put the container in another container.For example, split buttons and text boxes into two panels,Put these two panels and other buttons into the window.This method of constructing interfaces hierarchically,Ability to construct complex user interfaces incrementally.
Event-driven programming basics
1. Event, monitor, and monitor registration
Events on a graphical interface are user actions that occur on a component.For example, the user clicked a button on the interface,It is said that an event occurred on this button,This button object is the trigger of the event.Objects that monitor events are called monitors,Monitors provide methods for responding to events.To associate a monitor with an event object,Need to register monitors for event objects,Tells the system event object monitor.
Take the program responding to button events as an example,The program wants to create a button object,Add it to the interface,To register a button for a monitor,Programs need to have methods that respond to button events.When the "click button" event occurs,The system calls the event handler method registered for this button.Finish handling the button events.
2. The road to event handling
There are two main schemes for writing event handlers in the Java language:one is to reset the handleevent (eventevt).Another solution is for the program to implement some system-defined interfaces.Java provides multiple interfaces by event type,The class that is the monitor object needs to implement the corresponding interface,That is to implement the method of responding to events.When an event occurs,The handleevent (event evt) method set in the system automatically calls the event response method implemented by the monitor class.
The model used in the java.awt.event package to detect and react to events includes the following three components:
Source object:Event "occurs" on this component,It maintains contact with a set of objects that "listen" to the event.
Monitor object:an object of a class that implements a predefined interface,The object's class should provide methods for handling the events that occur.
Event object:It contains information describing the specific event passed from the source to the monitor when the event occurred.
What an event-driven program does besides creating source and monitor objects,You must also arrange the monitor to understand the source object,Or register the monitor with the source object.Each source object has a list of registered monitors,Provides a method to add monitor objects to this list.Only after the source object has registered the monitor,The monitor object is notified of events that occur on the source object.
3. Event types and monitor interfaces
In java language, in order to facilitate system management events,Also to facilitate program registration for monitors,The system classifies events,Called event type.The system provides an interface for each event type.The class to be the monitor object must implement the corresponding interface,Provides methods for responding to events specified by the interface.
Take the program response button event as an example,The jbutton class object button can be the trigger of an event.When the user clicks the button corresponding to the button in the interface,The button object will generate an event of type actionevent.If the monitor object is obj and the class of the object obj is obj, the class obj must implement the actionlistener interface in awt and implement the actionperformed method of monitoring button events. The button object must register its monitor obj with the addactionlistener method.
When the program runs,When the user clicks the button corresponding to the button object,The system passes an actionevent object from the event firing object to the monitor.The actionevent object contains information about which button the event occurred in,And other information about the event.
The following table shows some typical event types and some of the swing components that generate these events. When the actual event occurs,Usually produces a series of events,For example, the user clicks a button,A changeevent event will be generated to indicate that the cursor is on the button.Then there is a changeevent event indicating that the mouse was pressed,Then the actionevent event indicates that the mouse has been released,But the cursor is still on the button,Finally, the changeevent event indicates that the cursor has left the button.But applications usually only handle a single actionevent event for the complete action of pressing a button.
Each event type has a corresponding monitor interface,Table 11-2 lists the methods for each interface.A class that implements the monitor interface must implement all methods defined in the interface.
- Java Snake Game Writing Code
- Java programming snake game
- Java Snake Speed Edition
- Using doubly linked lists to implement greedy snake programs in java
- Java must learn GUI programming
- Java GUI programming list and combo box design use
- Tutorial for writing GUI dialogs in Java
- JAVA GUI custom JPanel artboard background
- Java simple guessing game code
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- xcode - pod install [!] no `podfile 'found in the project directory
- i want to call a child component method from a parent in vuejs
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning