Java interface Programming (top)

Source: Internet
Author: User
Tags set background

I. Introduction of AWT

1) AWT (Abstract Windows Toolkit) includes many classes and interfaces for the Java application GUI (graphics user interface graphical interface programming)

2) Various elements of the GUI (Windows, buttons, text boxes) are implemented by Java classes.

3) The classes involved in using AWT are generally in the java.awt package and its child packages.

4) Container and component are the two core classes in AWT.

All graphical elements that can be displayed are called Componnet,component, which represent all the visible graphical elements, and component have a special graphical element called container, Container (Container) is a kind of container that can hold other component elements in the graphical interface, Container itself is also a kind of component,container inside can accommodate other Container.

Container inside is divided into window and Pannel,window can be displayed independently, usually we see a variety of application windows can become Window,window as an application window independent display, Pannel can also accommodate other graphic elements, but generally invisible pannel,pannel cannot be displayed as a separate window for the application, and Pannel must be loaded into the window to display it.

Panne application is typically the applet (Java's page applet), now basically no more Ajax and Avascript completely replace its application.

Window itself can be divided into frame and dialog,frame is what we usually see the General window, and dialog is those who require the user to do certain actions (such as clicking on a drop-down menu item) only appear dialog box, this dialog is dialog.

Ii. Components and Containers

The basic component of Java's graphical interface is the Component,component class and its subclasses that describe GUI elements that graphically display on the screen and interact with the user, such as button, lable.

A generic Component object cannot be displayed independently, and must be "placed" in a container object before it can be displayed.

Container is a component subclass, container subclasses can "accommodate" other component objects.

The container object can use the method Add () to add additional component objects to it.

Container is a subclass of component, so container objects can also be added to other container objects as component objects.

There are two common types of container:

window: A top-level window whose object represents a free berth

Pannel: The object can be used as an additional component object, but cannot exist independently and must be added to other container (such as window or applet)

Frame

A frame is a subclass of Windows, and an object created by a frame or its subclasses is a form.

Common construction methods for frame:

Frame ()

Frame (String s) creates a window with a title bar of s (a static window rather than an action pop-out window)

Method

SetBounds (int x,inty,int width,int height);

SetSize (int width,int height);

setlocation (int x,int y);

SetBackground (Color c)

SetVisible (Boolean B)

Settitle (String name)

String GetTitle ()

Setresizeable (Boolean B)

Package Com.lost.frame;import Java.awt.color;import Java.awt.frame;public class Framedemo {public static void main ( String[] args) {//TODO auto-generated method Stubframe f1 = new MyFrame (100,100,400,400,color.blue);       Frame F2 = new MyFrame (300,100,400,400,color.yellow); Frame F3 = new MyFrame (100,300,400,400,color.green);     Frame f4 = new MyFrame (300,300,400,400,color.magenta);}} Class MyFrame extends frame{static int  id = 0;public myframe (int x,int y,int width,int height,color Color) {//TODO Au To-generated Constructor Stubsuper ("MyFrame" + (++id)); SetBackground (color); SetBounds (x, y, width, height); setlayout ( NULL); setvisible (True);}}

Pannel

Pannel objects can be seen as spaces that hold componnet

The Pannel object can have its own layout manager

The Pannel class has inherited from its parent class.

SetBounds (int x,int y,int width,int height)

setSize (int width, int height)

setlocation (int x, int y)

SetBackground (Color c)

SetLayout (LayoutManager Mgr)

Construction method

Pannel ()

Pannel (LayoutManager layout)

Package Com.lost.pannel;import Java.awt.color;import Java.awt.frame;import java.awt.panel;import Javax.lang.model.type.nulltype;public class Panneldemo {public static void main (string[] args) {//TODO auto-generated Me Thod Stubframe f = new Frame ("Java Frame with Pannel"); panel p = new Panel (); f.setlayout (null); F.setbounds (    .); F.setbackground (0,0,102),////Set background color with three primary colors (red, green, blue) to provision the background color    p.setbounds (50,50,400,400);//The coordinates set here ( 50,50) is a    p.setbackground (new Color (204,204,255)) relative to the frame form;    F.add (P);//The Panel container is loaded into the frame container so that it can be displayed in the frame window    f.setvisible (TRUE);}}

Third, layout manager

Manage the layout of component in container without having to set the location and size of the component directly

Each container has a layout management object that, when it needs to locate or size the Acura component, calls its corresponding layout manager and calls container's SetLayout method to change its layout manager object.

AWT provides 5 layout manager classes:

FlowLayout

BorderLayout

GridLayout

CardLayout

GridBagLayout

1. The first layout manager--------FlowLayout

FlowLayout is the default layout manager for the Pannel class

The FlowLayout layout Manager positions the components line-by-row, left-to-right, and line-wrapped.

Without changing the size of the component, the component is displayed in its original size, and you can set different component spacing, line spacing, and how.

The FlowLayout layout manager defaults to the way it is in play.

FlowLayout Construction Method

New FlowLayout (flowlayout.right,20,40)

Right-aligned, horizontal spacing between components 20px, vertical spacing 40px

New FlowLayout (Flowlayout.left)

Left-aligned, horizontal and vertical spacing is the default value of 5px

New FlowLayout ()

The default center alignment is the default value of 5 for horizontal and vertical spacing.

Package Com.lost.flowlayout;import Java.awt.button;import Java.awt.flowlayout;import Java.awt.frame;public class Flowlayoutdemo {public static void main (string[] args) {//TODO auto-generated method stubframe F  = new Frame ("Flowlay Out "); Button btn1 = New button ("Btn1"); Button btn2 = New button ("btn2"); Button btn3 = New button ("Btn3"), F.setlayout (New FlowLayout ()); F.add (BTN1); F.add (BTN2); F.add (BTN3); F.setvisible ( true); F.setsize (500, 500);}}

  

2, BorderLayout

BorderLayout is the default layout manager for the frame class

Boderlayout the entire container into

East (east) west (west) south (north) (CENTER)

The component can only be added to the specified zone.

If you do not specify the part of the component, you are added to the center area by default

Only one component can be added to each zone, such as adding multiple, the previous join will be overwritten

BorderLayout layout container size scaling principle

Horizontal and vertical Scaling

Package Com.lost.border;import Java.awt.borderlayout;import Java.awt.button;import Java.awt.frame;public class Borderlayoutdemo {public static void main (string[] args) {//TODO auto-generated method Stubframe f = new Frame ("Borderlay Out "); Button btn1 = New button ("East"); Button btn2 = New button ("West"); Button btn3 = New button ("North"); Button Btn4 = New button ("South"); Button btn5 = New button ("center"), F.setlayout (New BorderLayout ()); F.add (btn1,borderlayout.east); F.add (BTN2, Borderlayout.west); F.add (Btn3,borderlayout.north); F.add (Btn4,borderlayout.south); F.add (Btn5, Borderlayout.center); f.setvisible (true); F.setsize (500, 500);}}

3. GridLayout (Table layout Manager)

GridLayout Layout Manager

The GridLayout layout manager divides the space into regular, rectangular networks with equal sizes for each cell range. The components are added to each cell, and then a row is filled from left to right, and then from top to bottom.

Specify the number of rows and columns to split in the GridLayout construction method

New GridLayout (3,4)

Package Com.lost.grid;import Java.awt.button;import Java.awt.frame;import Java.awt.gridlayout;public class Gridlayoutdemo {public static void main (string[] args) {//TODO auto-generated method Stubframe f = new Frame ("GridLayout" ); F.setlayout (new GridLayout (2,3)); Button btn1 = New button ("Btn1"); Button btn2 = New button ("btn2"); Button btn3 = New button ("Btn3"); Button Btn4 = New button ("Btn4"); Button btn5 = New button ("Btn5");  Button btn6 = New button ("Bnt6"); F.add (BTN1); F.add (BTN2); F.add (BTN3); F.add (BTN4); F.add (BTN5); F.add (BTN6);  /* * Causes this Window is sized to fit the preferred size and layouts of its subcomponents. * The resulting width and height of the window are automatically enlarged if either of dimensions * was less than the M      Inimum size as specified by the previous call to the Setminimumsize method. If the window and/or its owner is not displayable yet, both of them is made displayable before calculating the Prefe rred size. The Window is ValiDated after it size is being calculated.  Set to fit Size * */f.pack (); F.setvisible (True);}}

Nested use small instances of frame

  

Package Com.lost.layout;import Java.awt.borderlayout;import Java.awt.button;import java.awt.color;import Java.awt.frame;import Java.awt.gridlayout;import Java.awt.panel;public class Layoutcombinedemo {public static void Main (string[] args) {//TODO auto-generated method Stubframe f = new Frame ("Layout nested Use") F.setlayout (New GridLayout (2, 1 ); F.setvisible (true); f.setlocation (+); f.setsize (+); F.setbackground (new Color (204,204,255)); panel p1 = new Panel (new BorderLayout ()); panel P2 = new Panel (new GridLayout (2,1)); panel p3 = new Panel (new BorderLayout ()); panel P4 = new Panel (new GridLayout (2,2));p 1.add (New button ("East (p1-)"), Borderlayout.east);p 1.add (The New button ("West" ( p1-, Borderlayout.west);p 2.add (New button ("P2-button1"));p 2.add (New button ("P2-button2"));p 1.add (P2, Borderlayout.center);//Add P2 as an element into P1 p3.add (New button ("East (p3-)"), Borderlayout.east);p 3.add (The New button ("West" (  p3-West), borderlayout.west); for (int i=0;i<4;i++) {P4.add (New button ("P4-button" +i));    } p3.add (P4,borderlayout.center);  F.add (p1); F.add (p3);}}

  

Iv. Summary of Layout Manager

Frame is a top-level window, and the default layout manager for frame is BorderLayout

The panel cannot be displayed separately and must be added to a container.

The panel's default layout manager is Fowlayout

When Pannel is added to a container as a component, the Panel can still have its own layout manager.

With the layout manager, the layout manager is responsible for the size and location of the individual components, so users cannot set the size and location properties of the component in this case, and if they attempt to use the setlocation setSize SetBounds provided in the Java language, they will be overwritten by the layout manager.

If the user does need to set the component size or location yourself, you should cancel the container's layout manager

SetLayout (NULL);

Java interface Programming (top)

Related Article

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.