Java Components and Containers

Source: Internet
Author: User
Tags visibility

The most basic component of the Java graphical user interface is the component (Component), which is an object that can be graphically displayed on the screen and interact with the user, such as a button, a label, and so on. Components cannot be displayed independently, and components must be placed in a certain container before they can be displayed.

Class Java.awt.Component are the parent class of many component classes that encapsulate the common methods and properties of a component, such as component objects, sizes, display positions, foreground and background colors, boundaries, visibility, and so on, in the component class. As a result, many component classes inherit the member methods and member variables of the component class, and the corresponding member methods include:

Getcomponentat (int x, int y)
GetFont ()
Getforeground ()
GetName ()
GetSize ()
Paint (Graphics g)
Repaint ()
Update ()
SetVisible (Boolean B)
SetSize (Dimension D)
SetName (String name), etc.
  
A container (Container) is also a class, actually a subclass of component, so the container itself is a component that has all the properties of the component, but its main function is to accommodate other components and containers.

Layout Manager (LayoutManager): Each container has a layout manager that calls its corresponding layout manager when the container needs to locate or determine its size size.

In order for the graphical user interface we generate to have good platform independence, the Java language provides a layout manager tool to manage the layout of components in a container without using a way to directly set the location and size of the components.

When arranging the location and size of components in your program, you should be aware of the following two points:
1. The layout manager in the container is responsible for the size and location of each component, so users cannot set these properties for the component in this situation. If you attempt to use setlocation (), SetSize (), SetBounds (), and so forth in the Java language, they will be overwritten by the layout manager.

2. If the user does need to set the component size or location himself, you should cancel the container's layout manager by:
SetLayout (NULL);

Common containers

Container Java.awt.Container is a subclass of component, a container that can hold multiple components and make them a whole. The container can simplify the design of the graphical interface and layout the interface with the whole structure. All containers can add components to the container through the Add () method.

There are three types of containers: Window, Panel, ScrollPane, commonly used with panel, Frame, Applet.

1. Frame
2. Panel

1. Frame

  

The following is an example of a container:

Example 5. 1
Import java.awt.*;
public class MyFrame extends frame{
public static void Main (String args[]) {
MyFrame FR = new MyFrame ("Hello out there!");
Construction method
Fr.setsize (200,200);
Sets the size of the frame, by default (0,0)
Fr.setbackground (color.red);
Sets the background of the frame, which defaults to red
Fr.setvisible (TRUE);
Set frame to visible, default to Invisible
}
Public MyFrame (String str) {
Super (STR); To invoke the constructor method of the parent class
}
}

Generally we want to generate a window, usually by using the subclass frame of window to instantiate, rather than directly using the window class. The frame looks like the windows we normally see under Windows, with headings, borders, menus, sizes, and so on. After an object instantiation of each frame, there is no size and no visibility, so you must call SetSize () to set the size, and call setvisible (true) to set the window to be visible.

In addition, AWT is in the actual running process is to call the platform of the graphics system, so the same section of AWT program operating under different OS platform to see the graphics system is not the same. For example, when running under Windows, the window displayed is a Windows-style window, and UNIX-style windows are displayed when running under UNIX.

2. Panel

  

Example 5. 2
Import java.awt.*;
public class Framewithpanel extends frame{
Public Framewithpanel (String str) {
Super (STR);
}

public static void Main (String args[]) {
Framewithpanel FR = new Framewithpanel ("Frame with Panel");
Panel pan=new Panel ();
Fr.setsize (200,200);
Fr.setbackground (color.red);
Frame FR's background color is set to red
Fr.setlayout (NULL);
Cancel Layout Manager
Pan.setsize (100,100);
Pan.setbackground (Color.yellow);
Set panel pan with yellow background color
Fr.add (PAN); Add the panel pan to the frame fr with the Add method
Fr.setvisible (TRUE);
}
}

Generally we want to generate a window, usually by using the subclass frame of window to instantiate, rather than directly using the window class. The frame looks like the windows we normally see under Windows, with headings, borders, menus, sizes, and so on. After an object instantiation of each frame, there is no size and no visibility, so you must call SetSize () to set the size, and call setvisible (true) to set the window to be visible.

In addition, AWT is in the actual running process is to call the platform of the graphics system, so the same section of AWT program operating under different OS platform to see the graphics system is not the same. For example, when running under Windows, the window displayed is a Windows-style window, and UNIX-style windows are displayed when running under UNIX.



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.