Java basics: GUI

Source: Internet
Author: User

Java basics: GUI
 
I. Overview

 

GUI is short for Graphical User Interfaces.

 

However, java does not have much advantage in GUI. The advantage of java lies in the network. This is just an introduction, so I will not discuss it in depth.

There are two essential conditions for GUI implementation: Components and events

Java encapsulates interface components as objects and places them in two packages: java. Awt and javax. Swing.

------ Awt package: Abstract Window Toolkit, which is the Abstract Window Toolkit. It is a heavyweight control that calls the local system method implementation function.

------ Swing package: a GUI system built on the basis of AWT. It provides more components and is fully implemented by java. It enhances portability and is a lightweight control.

 

Ii. awt package

1. Relationship between Container (Container) and Component (Component)

The two core classes in AWT are Container (Container) and Component (Component)

 

The most basic Component of the java graphical user interface is Component. The Component class and its subclass objects are used to describe GUI elements that are displayed graphically on the screen and can interact with users. For example: A button, a tag, etc.

Generally, Component objects cannot be displayed independently. They must be placed in a Container object.

 

Container is a subclass of Component. The iner subclass object can "hold" other Component objects or be added to other Container objects as Component objects.

You can use the add () method to add other Component objects to the Container object.

 

If you still don't understand it, it doesn't matter. Let me simply put it.

Whether the container (container), button, or label are components, other components can be added to the container.

 

2. Window and pannel

A, Window: its object indicates a top-level Window with free parking

B. Panel: its objects can be used as other Component objects, but they cannot exist independently. They must be added to other iner (such as Window or Applet)

3, Frame:

Frame is a subclass of Window, and an object created by Frame or its subclass is a form.

 

4. Example:

 

Frame f = new Frame (test form); f. setLayout (new FlowLayout (); f. setVisible (true); f. setBounds (600,200,500,400); Button B = new Button (one Button); f. add (B );
3. layout manager

 

 

Java runs across platforms, but different platforms have different definitions of vertices and coordinates. In addition, different screen resolution may also cause location changes. To ensure the relative position, size, and appearance of each component, java has designed a layout manager.

1. layout manager category:

FlowLayout: Floating layout manager

BorderLayout: border layout manager

GridLayout: grid layout manager

CardLayout: Card layout manager

GridBagLayout: Grid package layout manager

If you have done web development, you will feel that java layout management is a bit like web page layout. In fact, web pages are also running on different platforms and computers with different resolutions, from this point, it is indeed quite similar to java.

2, FlowLayout

Is the default manager of the Panel container. Its components are floating in the container, and its placement rules are from top to bottom, from left to right.

 

Public class FrameFlow {public static void main (String [] args) {// create a Frame f = new Frame (Test); // set the form size f. setSize (400,300); // you can specify the position f. set location (100,100); f. setBackground (Color. orange); // form background color f. setVisible (true); // The form is visible to f. setLayout (new FlowLayout (); // sets the floating Layout mode for (int I = 0; I <15; I ++) {f. add (new Button (btn + I ));}}}


 

3, BorderLayout

Deploy the border layout of the container. You can arrange the container components and adjust the size to make them conform to five areas: north, south, east, west, and middle. Each region can contain up to one component and is identified by corresponding constants: NORTH, SOUTH, EAST, WEST, and CENTER. If there is only one component, the default position is Center, it is the default layout manager for Windows, Frame, and Dialog.

 

 

Public static void main (String args []) {Frame f; f = new Frame (Border Layout); Button bn = new Button (North ); button bs = new Button (South); Button bw = new Button (West); Button be = new Button (East); Button bc = new Button (middle); f. add (bn, BorderLayout. NORTH); f. add (bs, BorderLayout. SOUTH); f. add (bw, BorderLayout. WEST); f. add (be, BorderLayout. EAST); f. add (bc, BorderLayout. CENTER); f. setSize (200,200); f. setVisible (true );}


 

 

The other layout implementation methods are similar. I will not write code one by one. You can try it on your own.

 

Iv. event listening Mechanism

 

Composition of the event listening mechanism:

Event: a user's operation on the Interface usually uses various input devices, such as the mouse and keyboard.

Event Source: The component that generates the event. For example, clicking a button is the event source.

Listener: contains the event processor, which is responsible for checking whether the event has occurred. If so, the event processor is activated to process it.

Event Processing: the processing method after an event is triggered.

 

 

Example:

 

Public static void main (String [] args) {Frame f = new Frame (test form); f. setLayout (new FlowLayout (); f. setVisible (true); f. setBounds (600,200,500,400); Button B = new Button (one Button); f. add (B ); /**************** add event listening mechanism *************** // Add and close button f. addWindowListener (new WindowAdapter () {// if you use a subclass to implement the WindowListener interface, you need to overwrite the seven methods. You can only use the close action, but not use other actions, but all of them must be rewritten. // Because the WindowLister subclass WindowAdapter has implemented this interface and overwritten all the methods. // You only need to inherit the WindowAdapter and overwrite the required method. @ Overridepublic void windowClosing (invalid wevent e) {System. exit (0) ;}}); // Click Event B in the Add button. addActionListener (new ActionListener () {// because the ActionListerer interface only implements one method, no Adapter is required. There is no ** Adapter class, // if there are more than two implementation methods, ** Adapterprivate int num = 0; @ Overridepublic void actionreceivmed (ActionEvent e) {num ++; System. out. println (click button + num );}});}


 

 

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.