Java--gui__java

Source: Internet
Author: User


The AWT (Abstract Windowtoolkit Window tool Group) includes many classes and interfaces for Java. Application GUI (Graphics user interface graphical user interface) programming, various elements of the GUI (such as: Windows, buttons, text boxes, etc.) are implemented by Java classes. The classes involved in using AWT are generally in the java.awt package and its sub subpackages

Swing is a GUI toolkit designed for Java. Swing is part of the Java base class. Swing includes a graphical user interface (GUI) device such as a text box, a button, a separator pane, and a table. Swing offers many better screen-display elements than AWT. They are written in pure Java, so they can be run across platforms like Java itself, unlike AWT.

To put it simply:

AWT is the abstract Window Component Toolkit, the earliest Java development package for programming graphics applications.

Swing is a newly developed package that addresses AWT's problems and is based on AWT.

Container and component are the two core classes in AWT




Component and Container


The most basic component of the Java graphical user interface is the Component,component class and its subclasses to describe GUI elements that are graphically displayed on the screen and interact with the user, such as a button, a label, and so on.

General component objects cannot be displayed independently, and must be displayed in a container object.

Relationship:


Container are component subclasses, container subclasses can "hold" other component objects, or they can be added to other component objects as container objects

Container object can add other component objects to it using method Add ()

There are two common types of container:


1,window: The top-level window whose object represents a free berth

2,panel: Its objects can be used to accommodate other component objects, but they cannot exist independently, and must be added to other container (such as Windows or applets)

Frame:

The frame is a subclass of window, and the object created by the frame or its subclasses is a form

Common construction methods of frame:

1,frame ()

2,frame (string s) creates a window with a title bar of string s


Example:

	Import java.awt.*;
	public class testmultiframe{public
		static void Main (String args[]) {
			myframe f1=new myframe (100,100,200,200, Color.Blue);
			MyFrame f2=new MyFrame (300,100,200,200,color.yellow);
			MyFrame f3=new MyFrame (100,300,200,200,color.green);
			MyFrame f4=new MyFrame (300,300,200,200,color.magenta);
			}
	Class MyFrame extends frame{
		static int id=0;
		MyFrame (int x,int y,int w,int h,color Color) {
			super ("MyFrame" + (++id));
			SetBackground (color);
			SetLayout (null);
			SetBounds (x,y,w,h);
			SetVisible (True);
		}
	



Panel:

A Panel object can be viewed as a space that can hold component

A Panel object can have its own layout manager


Example:

	Import java.awt.*;
	
	public class testmultipanel{public
		static void Main (String args[]) {
			new MyFrame2 ("Myframewithpanel", 300,300,400,300);
			}
	Class MyFrame2 extends frame{
		private Panel p1,p2,p3,p4;
		MyFrame2 (String s,int x,int y,int w,int h) {
			super (s);
			SetLayout (null);
			P1=new Panel (null);
			P2=new Panel (null);
			P3=new Panel (null);
			P4=new Panel (null);
			P1.setbounds (0,0,W/2,H/2);
			P2.setbounds (0,H/2,W/2,H/2);
			P3.setbounds (W/2,0,W/2,H/2);
			P4.setbounds (W/2,H/2,W/2,H/2);
			P1.setbackground (Color.Blue);
			P2.setbackground (color.green);
			P3.setbackground (color.yellow);
			P4.setbackground (Color.magenta);
			Add (p1);
			Add (p2);
			Add (p3);
			Add (p4);
			SetBounds (x,y,w,h);
			SetVisible (True);
			}
		




Layout Manager:

In the Java language, objects that provide layout manager classes can manage

1, manage the layout of component in container, do not have to set component position and size directly

2, each container has a layout manager object that, when the container needs to locate or determine the size of a component, calls the appropriate layout manager, calling the container setlayout method to change its layout manager object.

AWT provides 5 types of layout manager classes:
1,flowlayout

2,borderlayout

3,gridlayout

4,cardlayout

5,gridbaglayout

FlowLayout Layout Manager

1, is the default layout manager for the Panel class

2, this layout manager navigates the component line by row, from left to right, row after line

3, do not change the size of the component, according to the original size of the component display components, you can set different component spacing, line spacing and alignment

4, the default alignment is centered


Example:

Import java.awt.*;
public class testflowlayout2{public
	static void Main (String args[]) {
		frame f=new frame ("Java Frame");
		FlowLayout l=new FlowLayout (flowlayout.center,20,40);
		F.setlayout (l);
		F.setlocation (300,400);
		F.setsize (300,200);
		F.setbackground (New Color (204,204,255));
		for (int i=1;i<=7;i++) {
			f.add (New button ("button"));
			F.setvisible (True);
		}
		



BorderLayout Layout Manager

1, is the default layout manager for the frame class

2,borderlayout divides the layout of the entire container into five areas of East (eastern), West, south (SOUTH), North, Central (CENTER), and components can only be added to the specified area

3, if you do not specify the component's addition, the default is to join the center section

4, each area can only join one component, such as adding multiple, then the previously added will be overwritten

5, the principle of scaling: North and South two areas in the horizontal scaling, east and west two areas in the vertical scaling, the middle can be scaled in two directions


Example:

Import java.awt.*;
public class testborderlayout{public
	static void Main (String args[]) {
		Frame F;
		F=new Frame ("Border Layout");
		Button Bn=new button ("bn");
		Button Bs=new button ("BS");
		Button Bw=new button ("bw");
		Button Be=new button ("be");
		Button bc=new button ("BC");
		F.add (Bn,borderlayout.north);
		F.add (Bs,borderlayout.south);
		F.add (bw,borderlayout.west);
		F.add (be,borderlayout.east);
		F.add (bc,borderlayout.center);
		
		F.setsize (200,200);
		F.setvisible (True);
		}




GridLayout Layout Manager

1, this manager divides the control into a specified rectangular grid with equal size for each cell. Components are added to each cell, first adding a row from left to right, and wrapping, from top to bottom


Example:

Import java.awt.*;
public class testgridlayout{public
	static void Main (String args[]) {
		frame f=new frame ("GridLayout Example"); C3/>button b1=new button ("B1");
		Button B2=new button ("B2");
		Button B3=new button ("B3");
		Button B4=new button ("B4");
		
		F.setlayout (New GridLayout (3,2));
		F.add (B1);
		F.add (B2);
		F.add (b3);
		F.add (b4);
		
		F.pack ();  Wrap up
		f.setvisible (true);
		
		}




Layout Manager Summary:


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

2,panel cannot be displayed separately and must be added to a container. The panel's default layout manager is FlowLayout

3, when a panel is added to a container as a component, the Panel can still have its own layout manager

4, when using the layout manager, the layout manager is responsible for the size and location of individual components, so users cannot set the component size and location properties in this case, if the view uses the setlocation (), SetSize (), SetBounds (), and so on in the Java language, will be overwritten by the layout manager

5, if the user does need to set the component size or location himself, the layout manager for the container should be canceled by: setlayout (NULL)

The above is just a simple introduction to the GUI, let us have a simple understanding of it, with the subsequent study continues to deepen.




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.