AWT preliminary-Frame and Panel, awtframepanel

Source: Internet
Author: User

AWT preliminary-Frame and Panel, awtframepanel

GUI and AWT

GUI: graphical User Interface

AWT: Abstract Window Toolkit Abstract Window tool set

Previous program output results are displayed on the console. After learning AWT, you can program to display the graphic user interface.

Abstract Window Toolkit (AWT) is a graphical User Interface (GUI) tool set provided by API for Java programs. It is called Abstract Window toolset, this graphic user interface can be used across platforms for Java applets and applications. It supports graphic user interface programming.

  • Features:

User interface components;

Event processing model;

Graphics and image tools, including shapes, colors, and Fonts;

The layout manager can be used for flexible window la S, regardless of the size and resolution of a specific window;

Data transmission class, which can be cut and pasted through the clipboard of the local platform.

  • Java. awt package

The java. awt package provides the classes and interfaces used for GUI Design.
The java. awt package provides a GUI Design Tool for basic java programs. It mainly includes the following three concepts:

Component-Component
Container -- Container
LayoutManager

  • Components, containers, and layout manager

Components

The most basic Component of the Java graphical user interface is the Component, which is an object that can be displayed on the screen in a graphical way and can interact with users, for example, a button or a label. Components cannot be displayed independently. They must be displayed only in a certain container.

Common components:

Button:

TextField text box: accept user input

These components are encapsulated into classes, and instantiation is called directly during programming.

Class java. awt. component is the parent class of many Component classes. The Component class encapsulates common methods and attributes of components, including Component objects, sizes, display positions, foreground colors, background colors, boundaries, and visibility, therefore, many Component classes inherit the member methods and member variables of the Component class. 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) and so on

Container

 

A Container is also a class that is actually a subclass of Component. Therefore, the Container itself is also a Component and has all the properties of the Component, but its main function is to accommodate other components and containers.

Common Containers

The Container java. awt. Container is a subclass of Component. A Container can accommodate multiple components and make them a whole. The container can simplify the graphic interface design and arrange the interface with the overall structure. All containers can add components to the container by using the add () method.

There are three types of containers: Window, Panel, and ScrollPane. Commonly Used containers include Panel, Frame, and Applet.

1. Frame window
Inheritance relationship


  


  • Program Implementation
Public class Demo01 {public static void main (String [] args) {Frame frame = new Frame ("My first window"); frame. setBounds (50, 50,300,200); // set the Frame size, which is 50 from the left of the windows interface, and 300 frame in width and height. setBackground (Color. PINK); frame. setVisible (true); // set Frame to visible }}

Output:

 

Generally, we need to generate a Window, which is usually instantiated using the Frame subclass of the Window, rather than directly using the Window class. The Frame looks like a window that we usually see in windows, including the title, border, menu, size, and so on. After the object of each Frame is instantiated, there is no size or invisible. Therefore, you must call setSize () to set the size and call setVisible (true) to set the window to visible.

In addition, AWT calls the graphics system of the platform during actual operation. Therefore, the same AWT program runs on different operating system platforms and the graphics system is different. For example, when running in windows, the displayed window is a windows-style window, while running in UNIX, the displayed window is a UNIX-style window.

2. Panel

  

 

  • Program Implementation
Public class Demo02 {public static void main (String [] args) {Frame frame = new Frame ("My panel Interface"); frame. setBounds (50, 50,500,300); Panel panel = new Panel (); panel. setBackground (Color. pink); // panel. setBounds (0, 0,200,100); if you want to use a panel to block the entire window, you need to use the layout manager. You cannot directly call the implementation frame. add (panel); frame. setVisible (true );}}

Output:

 

A Panel can be used as a container to accommodate other components, but it cannot exist independently as a Frame and must be added to other containers.

  • Simple code implementation on the user login interface
Public class Demo03 {public static void main (String [] args) {Frame frame = new Frame ("user login window"); frame. setBounds (50, 50,400,100); Panel panel = new Panel (); panel. setBackground (Color. pink); frame. add (panel); Label lable = new Label ("User Name"); TextField textField = new TextField ("Enter the user name", 20 ); // 20 is the text box length. Button loginbtn = new Button ("OK"); panel. add (lable); panel. add (textField); panel. add (loginbtn); frame. setVisible (true );}}

Output:

 

Note that the components Lebel, TextField, And Button are added sequentially. The preceding figure shows a simple interface. in complex cases, you must use the layout manager. In addition, the above Code is relatively simple, only the basic display function is implemented, and the actual login and close operations are not allowed.


LayoutManager: each container has a layout manager. When the container needs to locate a component or determine its size, it calls its corresponding layout manager.

In Java, the layout manager is provided to manage the layout of components in the container, instead of directly setting the component position and size.

The layout manager mainly includes: FlowLayout, BorderLayout, GridLayout, CardLayout, and GridBagLayout.

1. FlowLayout (Stream layout)

FlowLayout is the default layout manager of the Panel and Applet (default layout manager ). Its component placement rules are from top to bottom and left to right. If the container is sufficiently wide, the first component is first added to the leftmost of the first line in the container, subsequent components are added to the right of the previous component in sequence. If the current row cannot be placed, the component is placed to the leftmost of the next row.

FlowLayout (FlowLayout. RIGHT, 20, 40 );
/* The first parameter indicates the alignment of the component. It indicates whether the component is in center alignment, right alignment, or left alignment. The second parameter indicates the horizontal interval between components, the third parameter is the vertical interval between components, in pixels. */
FlowLayout (FlowLayout. LEFT );
// Align left. Both the horizontal interval and the vertical interval are 5 pixels by default.
FlowLayout ();
// The default Alignment Method is center alignment. Both the horizontal interval and vertical interval are five pixels by default.

  • Code Implementation
    public static void main(String[] args) {        Frame frame = new Frame("FlowLayout");        frame.setBounds(100, 100, 400, 300);        frame.setLayout(new FlowLayout());                Button but1 = new Button("button1");        Button but2 = new Button("button2");        Button but3 = new Button("button3");        Button but4 = new Button("button4");        Button but5 = new Button("button5");                but1.setBackground(Color.blue);        but2.setBackground(Color.yellow);        but3.setBackground(Color.red);        but4.setBackground(Color.green);        but5.setBackground(Color.pink);                frame.add(but1);        frame.add(but2);        frame.add(but3);        frame.add(but4);        frame.add(but5);                frame.setVisible(true);            }}

Output:

The difference between the stream layout and the mahjong layout is that the mahjong layout should be configured in the east, west, north, and south directions, while the stream layout is arranged in sequence according to the call order of the buttons. By default, the queue is centered. To set it to left or right, you only need to instantiate FlowLayout and use its instance object fl to call the queue setting method and set it to left.

That is, FlowLayout fl = new FlowLayout ();
Fl. setAlignment (FlowLayout. LEFT );
Frame. setLayout (fl );

Left place.

2. BorderLayout (Mahjong layout)

BorderLayout is the default layout manager for Windows, Frame, and Dialog. The BorderLayout layout manager divides containers into five areas: North, South, East, West, and Center. Only one component can be placed in each area. Shows the location and size of each region:

    

  • Code Implementation
Public class Demo04 {public static void main (String [] args) {Frame frame = new Frame ("BorderLayt"); frame. setBounds (100,100,400,300); // set the frame layout to BorderLayout frame. setLayout (new BorderLayout (); Button btn1 = new Button ("button1"); Button btn2 = new Button ("button2 "); button btn3 = new Button ("button3"); Button btn4 = new Button ("button4"); Button btn5 = new Button ("button5"); btn1.setBackground (Color. blue); btn2.setBackground (Color. yellow); btn3.setBackground (Color. pink); btn4.setBackground (Color. green); btn5.setBackground (Color. red); frame. add (btn1, BorderLayout. EAST); frame. add (btn2, BorderLayout. NORTH); frame. add (btn3, BorderLayout. SOUTH); frame. add (btn4, BorderLayout. WEST); frame. add (btn5); frame. setVisible (true );}}

Output:

 

3. GridLayout (table layout)

Each component in the container is displayed in a grid layout, occupying the container space on average.

You should pay attention to the following two points when arranging the component location and hour in the program:

  • The layout manager in the container is responsible for the size and location of each component, so you cannot set these attributes of the component in this case. If you try to use methods such as setLocation (), setSize (), and setBounds () provided by Java, they will be overwritten by the layout manager.
  • If you really need to set the component size or position, you should cancel the layout manager of the container by setLayout (null );

 

  • Code Implementation
Public class Demo06 {public static void main (String [] args) {Frame frame = new Frame ("FlowLayout"); frame. setBounds (100,100,400,300); GridLayout gl = new GridLayout (,); // set the table to three rows and two columns, and the horizontal spacing of the table is 5 pixels, the Vertical spacing is 5 pixels frame. setLayout (gl); Button but1 = new Button ("button1"); Button but2 = new Button ("button2"); Button but3 = new Button ("button3 "); button but4 = new Button ("button4"); Button but5 = new Button ("button5"); but1.setBackground (Color. blue); but2.setBackground (Color. yellow); but3.setBackground (Color. red); but4.setBackground (Color. green); but5.setBackground (Color. pink); frame. add (but1); frame. add (but2); frame. add (but3); frame. add (but4); frame. add (but5); frame. setVisible (true );}}

Output:

  • Comprehensive application-implement a simple 4*4 calculator Interface
Public class Demo07 {public static void main (String [] args) {Frame frame = new Frame ("Calculator"); frame. setBounds (100,100,300,420);/* the code can be omitted because the default Frame layout is BorderLayout. BorderLayout bl = new BorderLayout (); frame. setLayout (bl); * // create a text input box and add it to the North position TextField tf = new TextField (); frame. add (tf, BorderLayout. NORTH); // create a Panel and set the layout to GridLayout Panel = new panel (); GridLayout gl = new GridLayout (,); // create a Panel, the size is 4*4, and the line spacing and column spacing are both 1 pixel panel. setLayout (gl); // Add the panel to the frame Center frame. add (panel, BorderLayout. CENTER); Button btn1 = new Button ("7"); Button btn2 = new Button ("8"); Button btn3 = new Button ("9 "); button btn4 = new Button ("+"); Button btn5 = new Button ("4"); Button btn6 = new Button ("5 "); button btn7 = new Button ("6"); Button btn8 = new Button ("-"); Button btn9 = new Button ("1 "); button btn10 = new Button ("2"); Button btn11 = new Button ("3"); Button btn12 = new Button ("*"); button btn13 = new Button ("0"); Button btn14 = new Button (". "); Button btn15 = new Button (" = "); Button btn16 = new Button ("/"); // Add the Button to the Panel panel Panel. add (btn1); panel. add (btn2); panel. add (btn3); panel. add (btn4); panel. add (btn5); panel. add (btn6); panel. add (btn7); panel. add (btn8); panel. add (btn9); panel. add (btn10); panel. add (btn11); panel. add (btn12); panel. add (btn13); panel. add (btn14); panel. add (btn15); panel. add (btn16); frame. setVisible (true );}}

Output:

  • Summary


1. Frame is a top-level window. The default layout manager of Frame is BorderLayout.

2. The Panel cannot be displayed separately and must be added to a container. The default layout manager of the Panel is FlowLayout.

3. After a Panel is added to a container as a component, the Panel can still have its own layout manager. Therefore, you can use Panel to display multiple components in a region of BorderLayout to design a complex user interface.

4. if you use setLayout (null), you must use setLocation (), setSize (), setBounds (), and other methods to manually set the size and position of the component, not encouraged.

 

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.