Java know how much (80) graphical Interface design basics

Source: Internet
Author: User

The previous program uses the simplest input and output method, the user enters the data in the keyboard, the program outputs the information on the screen. Modern procedures require the use of graphical user interface (graphical user Interface,gui), the interface has menus, buttons, etc., the user through the Mouse selection menu options and click the button, command program function module. This chapter learns how to write GUI science experiments in the Java language, and how to implement input and output through the GUI.

AWT and Swing

Previously, the GUI program was written in Java using the Abstract Window Toolkit, AWT (abstract Windows Toolkit). Now use swing more. Swing can be seen as an improved version of AWT rather than a substitute for AWT, which is an improvement and extension of AWT. So, when writing GUI programs, both swing and AWT work. they coexist in the Java Foundation Class (Java Foundation CLASS,JFC).

Although both AWT and swing provide classes that construct graphical interface elements, they differ in important ways: AWT relies on the main platform to draw user interface components, and swing has its own mechanism to draw and manage the interface component in the window provided by the main platform. The most obvious difference between swing and AWT is the look and feel of the interface components, and AWT runs the same program on different platforms, and there may be some differences in the appearance and style 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, 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, the main introduction is two packages:

    • Javax.swing Package: Contains the basic classes of swing;
    • Java.awt.event Package: Contains interfaces and classes related to handling events.


Swing is too rich to give a full introduction to swing in a textbook, but the knowledge about swing introduced in this chapter is enough to allow readers to write pretty GUI programs.

Components and Containers

components (component) are the basic elements of a graphical interface that users can manipulate directly , such as buttons. A container (Container) is a composite element of a graphical interface that can contain components, such as panels.

The Java language is a predefined class for each component, and the program passes through them or their subclasses of various component objects, such as the predefined button class JButton in swing, which is a class, the JButton object created by the program, 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, the predefined window class JFrame in Swing is a container class, and the object of the JFrame or JFrame subclass that the program creates is the window.

To unify the management of components and containers, define a superclass for all component classes and define common operations for the 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 be able to construct complex graphical interfaces hierarchically, containers are treated as special components, and containers can be placed in another container. For example, put a number of buttons and text boxes in two panels, and then put the two panels and other buttons into the window. This hierarchical method of constructing interfaces allows for the construction of complex user interfaces in an incremental manner.

Event-Driven Programming basics

1. Event, monitor, and monitor registration
An event on a graphical interface refers to a user action occurring on a component. For example, when a user clicks on a button on the interface, it says that an event occurred on the button, which is the trigger of the event. The object that monitors events 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, the event object needs to be registered as a monitor to tell the system event object's monitor.

The program responds to a button event, for example, to create a button object, add it to the interface, to register the button as a monitor, the program should have a way to respond to button events. When the Click Button event occurs, the system invokes the event handling method that has been registered for this button to complete the work of handling the button event.

2. Implementation of event-handling features
There are two main scenarios for writing event handlers in the Java language: one is a program reset Handleevent (EVENTEVT), and the program is slightly larger; the other is a program that implements some system-set interfaces . Java provides multiple interfaces by event type, and the class that acts as a monitor object needs to implement the appropriate interface, the method that implements the response event. When an event occurs, the Handleevent (event evt) method that is built into 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, which maintains contact with a set of objects that "listens" for the event.
    • Monitor object: An object that implements the class of a predefined interface that provides a way to handle the event that occurs.
    • Event object: It contains information that describes a specific event that is passed from the source to the monitor when an event occurs.


An event driver to do the work in addition to creating the source object and the monitor object, 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. An event that occurs on the source object is notified to the monitor object only after the source object has registered the monitor.

3. Event type and monitor interface
system provides an interface for each event type. The class to be used as the monitor object must implement the appropriate interface, providing a way for the interface to respond to events.

Then, for example, the program responds to a button event, the JButton Class object button can be the activator of an event. When the user clicks on the button in the interface that corresponds to buttons, the button object produces an ActionEvent type of event. If the monitor object is obj and the class of object obj is obj, then 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 with the addActionListener method. When the

program runs, a ActionEvent object is passed from the event firing object to the monitor when the user taps on the button object's corresponding buttons. The ActionEvent object contains information about which button the event occurred and other information about the event.

Table 11-1 gives a representative list of event types and some swing components that produce these events. When the actual event occurs, a series of events is usually generated, for example, when the user taps the button, the ChangeEvent event prompts the cursor to the button, and then another 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, indicating that the cursor has left the button. However, an application typically only handles a single ActionEvent event that presses the full action of a button.

Table 11-1   Components and event types
Event Type component description
actionevent jbutton,jcheckbox
Jcombobox,jmenuitem
Jradiobutton
Click, Options, or select
changeevent jslider Adjust the position of a moveable element
Adjustmenteven T jscrollbar Adjust the slider position
itemevent jcombobox,jcheckbox
Jradio Button
JRadioButtonMenuItem
JCheckBoxMenuItem
Select an item from a set of optional scenarios
Mesh
listselectio Nevent JList option event
keyevent
MouseEvent
jcomponent and its derived classes manipulate mouse or keyboard
careevent jtextarea,jtextfield Select and edit Text
windowevent window and its derived classes JFrame Open, Close, and icon Windows

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.

Table 11-2 Monitor interface Methods
Monitor Interface Method methods to get event sources
ActionListener Actionperformed Getsource,getactioncommand
ChangeListener StateChanged GetSource
Adjustmentlistener Adjustmentvaluechanged Getadjustable
Focuslistener Focusgained,
Focuslost
ItemListener Itemstatechanged Getitemselectable (), GetSource ()
Listselectionlistener ValueChanged E.getsource (). Getselectedvalue ()
KeyListener Keypressed,
Keyreleased,
keytyped
Carelistener Careupdate
MouseListener Mouseclicked,
Mouseentered,
Mouseexited,
Mousepressed,
Mousereleased
Mousemontionlistener Mousedragged,
Mousemoved
WindowListener WindowClosed,
Windowclosing,
Windowdeactivated,
Windowdeiconified,
Windowiconified,
WindowOpened


Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its application

Java know how much (80) graphical Interface design basics

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.