Graphical interface 1 in Java (j2se entry 12)

Source: Internet
Author: User

GUI in Java

Gui, graphical user interface, used for human-computer interaction.

Create a graphical interface

1. Select a container

2. Set the container layout manager.

3. Add components to the container

4. Add event listening

Container(Container) the object used to manage other components. The component must be placed in the container.

Jframe, This is a top-level form container, and all other components must be placed in the top-level container.

JdialogThe dialog box container depends on its parent component. It is not a top-level container.

JpanelIt is not a top-layer container and must be placed in the top-layer container. Any container has the add () method. The Panel is transparent (default) and is also a component.

Layout management: The setlayout () method is available in any container class. Call this method with the container object to set the container layout manager (layoutmanager is an interface, all layout managers have implemented this interface ).

Available layout manager:

Flowlayout, Streaming layout manager. The default layout management of a panel is flowlayout.

Borderlayout, Layout management by orientation, (north, south, east, west, middle) is not explicitly specified, it will be loaded in the middle by default (middle), add (Component comp, string place) this method adds a component at the specified position.

Gridlayout, Grid layout, through the row and column, spacing, to use the grid to split, put components into a grid, such as, first put the components in the back column.

Cardlayout, Card layout, overlapping panel placement.

GridboglayoutThe widget can be laid out across rows and columns in a grid.

Note:You must set the visibility only when other functions of the graphic interface are set..

Jbutton: button

Jtextfield: single-line text field

Jtextarea: multi-row Region

Jscrollpane: Rolling form

Jcombobox: drop-down box

Jradiobutton: Single-choice button

Jcheckbox: Select multiple buttons

Jlist: multi-row list

Jlabel: Label

Jpasswordfield: password input box

Jeditorpane: displays structured documents

Border: border

Jmenubar: menu bar

Jmenu: Menu

Jmenuitem: menu item

Jpopupmenu: pop-up menu

Jslider: slide bar

Jprogressbar: progress bar

Jtabbedpane: layered panel

Jsplitpane: Separation panel

Jtoolbar: tool bar

Jfilechooser: file Selector

Jcolorchooser: color selector

Show dialog box

There are many static methods in joptionpane to pop up the dialog box

AWT event model (Observer mode) (important)

In the event model, includingEvent source object,Event Handler(Event listener object ),Event object.

An authorization relationship is established between the event source and event handler, that is, an event handler object in the event source class is used as an attribute, or a collection of event handlers..

Event object

Event source--> event handler

This is the mechanism of the event model, that is, a message (event object) is sent by the event source object, and then the event handler calls the corresponding method to process the event.

In the eventListener InterfaceAll methods defined inTake the event object as the parameter.

An event source can register multiple listeners of the same type, multiple Event Listeners can also be registered, and one event listener can also serve multiple event sources.

First, let's take a look at the three types of message sending: A, B, and C, which serve as the event source, B event handler, and C event object. Class A has a class B attribute or a set of Class B objects, that is, the authorization relationship between the event source and the event handler, class B needs to implement a custom interface, which inherits eventlistener and does not define any methods in the eventlistener interface, which is just a tag interface. The Implementation defines the methods used for event processing in the Custom interface. Class C must inherit the eventobject class. These methods are B (c) with the event object as the parameter, and then the B (C) in Class A (c) method is called using class B objects) method, and take the event object as the parameter, and call the (c) method with the object of Class A in the main method, which is called a class object to send messages to Class B.

That is to sayThe event source object indirectly calls the event listener method and transmits the event object as the real parameter to the event listener method, call the event source to send a message (event object) to the event listener method).

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.