Preliminary __java of Java GUI programming

Source: Internet
Author: User


In practical applications, many of the application interfaces we see are GUI graphical user interfaces. Such as: We click on the QQ icon, will pop up a QQ Login interface dialog box. This QQ icon can be referred to as a graphical user interface.

In fact, the type of user interface is divided into two categories: command line userinterface (command-line user interface)

• Is a common DOS command line operation.

• Need to memorize some commonly used commands, the operation is not intuitive.

Example:

• For example: Create a folder, or delete a folder, etc.

such as MS-DOS

Second, the graphical user interface (gui,graphical user Interface) refers to the graphical way to interact with the user's program running interface, such as MicrosoftWindows, Word, and so on.

Advantages: More friendly, richer, and provide flexible, powerful man-machine interaction function, become the mainstream of current application design.

In GUI programming, components (Component) are the basic elements of the GUI, and objects that can be graphically displayed on the screen and interact with the user are components.

The following figure: components that are all GUI interfaces

In addition, a variety of GUI component classes, such as window, Menu, Button, Label, TextField, scrollbar, are defined in the JDK's java.awt package.

These abstract classes, which define the basic features and functionality of the GUI component

As can be seen from the above illustration, the components in the GUI can be divided into:

One of the things that needs to be noted is:

1. The container class describes all the properties of the container assembly;

2. It inherits from the component class, so the container class object itself is also a component that has all the properties of the component, but in turn the component is not necessarily a container;

3. The control component must be placed in the container component to be displayed

Among them, the container type component can be seen under the java.awt package. Here, note that there are two main types of containers in AWT:

1. Java.awt.Window
Describes a top-level container that has no borders and a menu bar and is freely docked (meaning it is not allowed to be included in other containers) and is not used directly, but uses its subclass frame.

2.java.awt.panel
The simplest and most commonly used container, which can contain other components as a container, cannot exist independently and must be added to other containers.

Case:

Example 1: The first GUI application.

importjava.awt.*;

classtestfirstframe{

public static void Main (String args[]) {

Frame Frame=new Frame ("First graphical user Interface application");//Container

Label Lbl=new label ("This is my first graphical user interface.") ");//Control component

Lbl.setbackground (Color.pink);

Frame.setlayout (New FlowLayout ());

Frame.add (LBL); To add a control component to a container

Frame.setsize (200,100);

Frame.setvisible (TRUE);

}

}

It should be noted that:

1. The default size of frame is to fit the title bar and the minimum (large), Close button, SetSize () to set the frame size.

2.Frame windows are invisible by default and can be made visible or hidden by using the setvisible (True|false) method.

3. The placement of the component in the container is determined by the layout manager, and frame uses the SetLayout () method to set the layout of the window.

4.flowlayout-flow layout management, characterized by the component in the container in the order of the entry line by row, row from left to right, row after row full line.

Example 2. Use of the container component panel.

Frame Frame=new Frame ("Use of the container panel");

Panel panel=new Panel ();

Button Btn=new button ("OK");

Panel.setbackground (Color.cyan);

Panel.setsize (100,50);

Panel.setlocation (40,40);

Frame.setlayout (NULL);

Frame.add (panel);

Panel.add (BTN);

Frame.setlocation (80,100);

Frame.setsize (200,100);

Frame.setvisible (TRUE);

Here, attention should be paid to:

1. Frame's default layout manager is canceled, and the panel size and position in the container are set manually;

The 2.setSize () method is used to set the size of the component, that is, the width and height, in pixels;

The 3.setLocation () method is used to set the position of the component in the container, that is, the upper-left corner of the component, the upper-left vertex coordinate of the component, or pixel.

4. Each GUI container has its own coordinate system (the computer's monitor screen is also a GUI container)

Position relationship as shown:


As we can see from the above figure, the position of layout in GUI programming is very important. In GUI programming, the arrangement of the container for the contained components, including the position and size of the component, is called the layout of the container (Layout). It refers to the system defined in advance a number of container layout effects, use them to easily implement the components in the container layout management, and to meet the various general needs. For example, FlowLayout and so on.

Attention:

Each container has a default layout manager, and when you create a container object, a corresponding default layout manager object is created, and the user can create and set up a new layout manager for the container at any time.
Method:

 

Container object. setlayout (Layout Manager Object)
Layout Manager Container object. GetLayout ()

Therefore, it is important to understand the common layout manager

FlowLayout: Streaming layout is the default layout manager type for the panel (and its subclasses) type container.

Layout effects: Components are positioned in the container in the order they are added, from left to right, and line by line. The component is displayed in its original size.

Construction method
Public FlowLayout ()//default center alignment, 5 pixel horizontal and vertical spacing
public flowlayout (int align)//Specify alignment
Public FlowLayout (int align,int hgap,int vgap)/component horizontal and vertical spacing

Where the alignment can be set using a static constant defined in the FlowLayout class, mainly includes:
Flowlayout.left left-aligned
Flowlayout.right Align Right
Flowlayout.center Center Alignment

Example 1: Use of streaming layouts.

F.setlayout (New FlowLayout ());

F.add (button1);

F.add (button2);

F.add (Button3);

Effect as shown:

      

Attention:

When the size of the container f is reset, the position of the component is also adjusted, but the size of the component remains unchanged.

BorderLayout: Boundary layout, is the default Layout Manager for window and its subclass type container.

Layout effect: Divides the entire container range into east, west, South, North, center five areas, and components can only be added to the specified area.

As shown in figure:

It is noteworthy that:

1. Only one component can be added to each zone, and if multiple is added, the previously added component will be discarded.

2. In a container that uses a border layout, the dimensions of the component are also forcibly controlled by the layout manager, which is the same size as the area in which it is located.

Construction method
Public BorderLayout ()
public borderlayout (int hgap,int vgap)

Example 2:borderlayout is used.

F.setlayout (New BorderLayout ())//This statement can be removed, frame default layout is BorderLayout

Add components to different orientations of the container, or use string constants defined in BorderLayout, for example, north, SOUTH, west, east, and so on.

F.add (Btnnorth, "North");

F.add (Btnsouth, "South");

F.add (Btnwest, "West");

F.add (Btneast, "East");

F.add (btncenter, "center");//If the component is not named, join the center area by default.

Attention:

1. When the size of the container changes, the relative position of each component is unchanged and the size is adjusted with the area.

2. Adjustment principle: North, South Two areas can only be scaled horizontally (width adjustable), East and west two areas can only be scaled vertically (height adjustable), the middle area can be scaled.

In the actual learning process, we need to understand the basic attributes of the Conpoment class and how to do it:


Understanding these properties and methods is very helpful for us to learn GUI graphical user interface development.

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.