The GUI represents the graphical user interface (graphical user Interface), and the Java API provides two sets of component user support for writing graphical user interfaces, including AWT and swing.
AWT uses the local operating system's code resources, known as heavyweight components.
Swing is built on the basis of AWT and uses the same event handling mechanism as AWT. Swing components are lightweight GUI components that are written entirely in plain Java code and do not rely on any particular platform to design component classes using MVC architecture. It divides the presentation layer into three separate parts, namely model, view, controller (controler). The model stores the data that defines the component, the view creates a visual representation of the component from the data in the model, the controller processes the user's interaction with the component, and, if necessary, changes the model or view in response to user behavior.
Swing is also known as a model-delegate schema or a detached model schema when developing swing because of the strong dependency of views and controllers, and the combination of view and controller as a logical entity for UI delegates.
Containers and Components
A container is a component that is used to place other components of a particular type, and the component is a standalone visual control. A component to be displayed must be placed in a container, the container is also a component, and the container can be placed in other containers.
Containers are divided into heavyweight containers and lightweight containers.
Heavy-weight containers:
Includes JFrame (form), JDialog (dialog box), JWindow (window), JApplet (applet). Because they inherit AWT's container, they become heavyweight containers. Each program must have at least one heavyweight container to provide a graphical user interface.
Each containment level must start with a heavyweight container and the heavyweight container cannot be contained in any other container. We are exposed to a hierarchy panel (which is divided into a content panel and an optional menu bar), and swing components are placed in the content panel.
Lightweight containers:
Includes commonly used panel JPanel, divider panes, scrolling panes, tab panes, toolbars, and the like, which inherit from the jcomponent of the swing class as normal components, making it a lightweight container. They exist primarily to locate other components.
Component:
button (class JButton), radio button (class Jradiobutton), check box (class Jcheckbox), combo box (class JComboBox), list box (class JList), Text field (class JTextField), The plain text area (class JTextArea) is inherited from the jcomponent of the Swing class.
Layout of the component in the container:
Components must be added to the container in order to be displayed in the program, depending on whether the layout manager is used and what type of layout manager is used.
If you need to locate the component precisely in the container, you must use the container's setlayout (null) method, and then call the component's SetBounds () method to set the component's bounds.
Components with precise positioning do not support window changes.
AWT and swing API built-in Layout manager
Streaming layout manager flowlayout (JPanel component defaults to FlowLayout layout manager):
Install components Add order from left to right, you can control the container component center, left, and right, you can control the vertical and horizontal spacing of components.
BorderLayout Manager (not used to place components, often used to place intermediate containers):
It is divided into five regions in north and south, of which the middle area and power are greatest.
JFrame the default manager.
Event-driven programming
The programming of code execution based on event occurrence is called event-driven programming.
Example: by clicking on a button to trigger an event, the button is the event source for the event, the listener is registered to the button, and when an action event occurs on the button, the button detects the listener by invoking the actionperformed () method of the defined listener.
Events and event sources
When running a Java GUI program, the program is responsible for interacting with the user, while the event is the driver's execution. Events are used to describe what has happened and can be triggered by the behavior of an external user, such as mouse movement, button clicks, typing characters, etc., or triggered by internal activity of the program, such as changes in the data model, which can optionally respond or ignore events.
The component that generates an event and triggers it becomes the source of the event. An event is an instance object of an event class, and the root object of the event class is Java.util.EventObject.
Event listeners, registering, and handling events
An event source triggers an event but the event source does not handle it, but instead delegates the object that is interested in the event to handle. This object, which is entrusted to handle events, is called an event listener.
The method of becoming an event listener on the event source:
1. Create a Listener object listener object must be an instance of the corresponding event listener interface. Java provides a listener interface for each type of event.
2. Register the Listener object on the event source.
Java Graphical user interface programming