GUI summary in the Java language

Source: Internet
Author: User
Tags event listener

First, the GUI

1, the concept of the GUI

The GUI (graphical user Interface) is a graphical UI that makes the application look more friendly.

2. AWT, swing, and SWT

AWT (Abstract Windows Toolkit) is the most primitive GUI toolkit in the Java language, and the related APIs are located in the java.awt package. AWT is a very limited GUI toolkit, such as trees, tables, etc. are not supported;

However, AWT was not able to achieve it. In the AWT runtime, each component relies on the GUI peer (peer) control of the current platform, so the appearance and behavior of the AWT GUI will depend on the current platform.

3. java.awt Bag

The java.awt package is a Java built-in package that is part of the Java Base Class Library (JFC), which includes the following content;

A rich set of interface components that facilitate user input;

Several layout managers that place components in the appropriate location;

Event processing model;

Graphics and image tools, and more.

4. Common Basic Swing components

In swing programming, there are a number of components that are often used, including:

JFrame (form, frame), JPanel (Panel, container), JButton (button), JLabel (label), JTextField (text box), JTextArea (text field).

5. Summary of steps to create a GUI

Analyze the components that need to be used in the GUI;

Divide the GUI into several sections, each using the JPanel layout. Each jpanel can use a different layout manager depending on the situation;

Layout multiple jpanel onto a jframe.

Second, swing event monitoring Quick Start

1. Event handling

Event control is very important for a program that uses a graphical user interface.

So far, we have written a graphical user interface program is just the completion of the interface, without any actual function, to achieve the corresponding function, the event must be handled;

The user interacts with the GUI component and events occur, such as pressing a button, typing a character with the keyboard, clicking the mouse, and so on;

What we are now focusing on is not "how events are produced", but rather about how we should "deal with them" when events occur.

2. Event Processing Model

In Java, the basic idea of event handling is as follows:

A source (event source) produces an event (event object) and sends it to the listener where the listener simply waits until it receives an event, and once the event is accepted, the listener handles the events;

An event source must register the listener so that the listener can accept notifications about a particular event.

3. Event sources and Events

When you click the mouse or press the keyboard in a graphical user interface, it is for the specific components of the action, such as: Using the mouse to click on a button, press the keyboard to enter the text box and so on;

We refer to the object manipulated by the action as the source of the event, please note: The event source must refer to a component;

An event is generated when an action is taken against the event source.

4. Listener and Listener interface

For each type of event, there is a corresponding listener for monitoring the occurrence of events;

In Java, the listener is provided by a series of interfaces;

In fact, the event listener is the class that implements the event listener interface, and the listener constantly listens to the action of the event source, and when the event source generates an event, the listener receives the notification of the event source and invokes a specific method to execute the specified action;

Specific event listeners are only interested in specific events.

5. Event Type

In fact, events are type-sensitive, such as mouse events that occur when the mouse is manipulated, keyboard events are generated when a character is entered using the keyboard, form events occur when the form is turned on or off, and so on;

For different types of events there will be different types of listeners corresponding to them;

The Java.awt.event package contains a series of listener interfaces that are used to handle different types of events, respectively.

6. Event Object

All events in Java are encapsulated in the event object, and all event objects derive from the EventObject class;

For different event types there will be different event objects, which are named in a manner similar to xxxevent, such as: ActionEvent, MouseEvent, and so on;

The event object contains information about what happens when an event occurs (that is, some data generated when the event is triggered), which is passed to the event handler by the event's underlying mechanism;

The event object is actually a parameter passed in the event handler, and can be obtained from the event object if it is necessary to use some relevant data during the process of handling the event.

Iii. Internal class

1, definition.

An inner class is a syntax structure unique to Java that defines another class within one class, such as:

public class Exteriorclass {

Private class Interiorclass {

}

}

At this point, the inner class Interiorclass becomes a member of the external class exteriorclass, and access rights follow the access mechanism of the class member, which can be public, protected, default, and private;

Inner classes can easily access other members of the outer class;

Internal classes are primarily designed to address the problem of mutual access among class members.

Iv. Anonymous Internal classes

Anonymous class is actually a kind of more special inner class, but this class has no name;

An anonymous class is similar to an inner class and has access to all members of an external class;

In many cases (especially in event handling), an anonymous class is generally defined in a method of an external class, so it is also called a local inner class, and for a local inner class, it can also access the parameters of the method;

When appropriate, using anonymous classes to implement event processing makes the code more concise and more flexible;

Can only be used once.

V. Concurrent tasks in the GUI

In the GUI, it is often possible to handle concurrent tasks, for example, a chat room should be able to support the ability to pass files, while the delivery of files can continue to chat;

Modify the empty chat log event processing to make it die loop, simulation takes a long time, visible in the cleanup process, can not do other operations;

Using multithreaded programming, will solve the problem, follow-up learning.

GUI summary In the Java language

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.