Core Java 8~9 (GUI & AWT event handling mechanism)

Source: Internet
Author: User
Tags event listener gettext response code

MODULE 8 GUIs
--------------------------------
Packages in the GUI:
java.awt.*;
javax.swing.*;
java.awt.event.*;


Requirements:
1) Understand the development process of GUI;
2) Master the common layout manager

Steps to develop GUI graphical interface
-------------------------------
1. Select the container
1) Frame Container
There are buttons such as Zoom out/maximize/close, top level container
2) Dialog dialog box
Usually depends on the container, does not appear alone
3) Applet relies on browser support, obsolete
4) Panel Lightweight small container
There are no Zoom out/maximize/close buttons, usually a large container needs to be set in a variety of arrangement styles, the large containers are cut into multiple small containers, each small container to add their own unique style. No scroll bar
5) ScrollPane small container with scroll bar

2. Set the layout manager for the container
Layout Manager determines the arrangement style of container components
1) setlayout (layout manager) Settings layout Manager
2) The position, size, and boundary of the component in the container can be set by means of the component's setlocation () setbounds (), etc.
3) All containers have a default layout manager
Window defaults to the same as the Borderlayout,frame container
Panel defaults to FlowLayout

3. Adding components to the container
1) Creating a Component Object
Button B=new button ("Add");
2) Adding components to the container
Add (b);

4. Create an event listener
Add the appropriate time processing code to each component
Westbtn.addactionlistener (new ActionListener () {
public void performed (ActionEvent e) {
Centerarea.settext (Centerarea.gettext () +westtxt.gettext () + "\ n");
Westtxt.settext ("");
}
});


Two ways to create a container
1) Use JFrame as a member variable
2) can inherit JFrame


Common layout Manager
------------------------
1.flowlayout Flow-style layout management
The components are arranged in rows, row by row, or the window is changed to wrap the lines automatically

2.BorderLayout
Divide the container into five major areas:
Borderlayout.center
Borderlayout.east
Borderlayout.west
Borderlayout.south
Borderlayout.north
Only one component can be placed in each zone, and the component fills the entire area
If you change the window size, only the center area will follow the change, and the rest of the area is unchanged

Exercise: Borderlayoutex.java


3.GridLayout Grid Layout Manager
Divides the container into several rows of the same size grid, each grid allows only one component, and the component fills the grid
No sense in resizing components

4.CardLayout Card-like layout manager
Set up multiple cards with only one component per card, and the component fills the entire area

JFC
Provides a common class library development graphical interface, operating system agnostic

Composition: AWT java2d accessebility Drag and Drop, Swing


MODULE 9 AWT event handling mechanism
--------------------------------
button click to generate a response
Event Source (trigger) Click event (Give) event listener ===> Execute response code

Three elements:
Event Source: Events sources
An action component, such as a button, generates an event
Event object:
Event objects triggered on a component, such as a mouse click, wrapping an action as an object
Event Listener: Events Listener
Responsible for responding to events originating from different event sources

Cases:


Analysis:
1) Event Source to maintain a list of listeners, the resulting events pass these listeners
2) The event object should carry information about the event source.
3) Event listener to know the source of the event and respond differently depending on the source of the event


Emotional models
----------------
Girls have emotional events sent to boys

Girl Event Source
1) responsible for generating emotional events happy ()/sad (), passing the emotion to all the boys interested in her
2) To maintain a child list of boys, record all the boys interested in her
3) Provide the appropriate method for registering listeners or deleting listeners

Emotionevent Event Object
Emotional events to carry information with girl.

Boy Listener
React differently to the emotional events of different girl

Adapter Adapter
-----------------------------------
Design reasons:
When writing event-handling code in a way that implements an event listener interface, all events in that interface must be implemented (many events will not be used). Consider simplifying this approach: adapter classes


Original: MouseListener Interface:

Core Java 8~9 (GUI & AWT event handling mechanism)

Related Article

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.