Java基础(十五)-----GUI基础①(一)

2014-11-24 02:31:42 · 作者: · 浏览: 0

java.awt包的结构

1,所有容器 组件的父类都可追溯至Component或者MenuComponent。
Component是所有的容器的父类,包括Button,TextField,Container.MenuComponent
则代表了图像界面的菜单组件,MenuBar,MenuItem,Menu.
2,Container是Component的子类,主要用来盛装其他的GUI组件。
3,Container主要提供了三种容器:Window和panel,Scrollpane.Window是可以独立存在的顶级窗口,
Panel是可以盛装其他组件,但是不能独立存在的容器。
4,Window的子类有:Frame,Dialog。Frame是顶级容器,Dialog是对话框。
5,Frame,Panel,ScrollPane:
Frame可以独立存在的顶级容器,默认布局方式:方位布局(BorderLayout)
Panel不能独立存在的容器,默认布局方式:流式布局(FlowLayout)
ScrollPane带滚动条的容器,,默认布局方式:方位布局(BorderLayout) 图示1:AWT图像组件的继承关系图: \ 图示2:AWT容器继承关系图: \

布局管理器

FlowLayout

组件从从左至右排列,遇到容器边缘,向下另起一行。代码示例:
package com.test;

import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;

public class FlowLayoutDemo1 extends Frame {
	
	public void init()
	{
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		for(int i=1;i<=12;i++)
		{
			this.add(new Button(""+i));
		}
		this.setSize(100,200);
		//this.pack();
		this.setVisible(true);
		
	}

	public static void main(String[] args) {
		
		new FlowLayoutDemo1().init();;

	}

}

BorderLayout

设置容器为BorderLayout可以将容器分为5部分:Center,North,South,West,East。代码示例:
package com.test;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;

public class BorderLayoutDemo extends Frame{
	
	
	public void init()
	{
		this.setLayout(new BorderLayout());
		this.add(new Button("中"),BorderLayout.CENTER);
		this.add(new Button("南"),BorderLayout.SOUTH);
		this.add(new Button("北"),BorderLayout.NORTH);
		this.add(new Button("西"),BorderLayout.WEST);
		this.add(new Button("东"),BorderLayout.EAST);
		
		this.setVisible(true);
	}

	public static void main(String[] args) {
		
		new BorderLayoutDemo().init();

	}

}

GridLayout

通过设置网格布局管理器,可以将容器分为大小相同的网格。添加组件的时候,默认是从左至右,从上往下添加组件。代码示例:
package com.test;

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;

public class GridLayoutDemo extends Frame {
	
	public void init()
	{
		this.setLayout(new GridLayout(3,2));
		for(int i=0;i<6;i++)
		{
			this.add(new Button(""+i));
			
		}
		
		this.setVisible(true);
		
	}

	public static void main(String[] args) {
		
		new GridLayoutDemo().init();

	}

}

GridBagLayout

一种可以设置组件横向,纵向占据空格和其他属性的布局管理器。
gridx,gridy:设置受该布局影响的横向和纵向的索引。默认值都为0;
gridWidth,gridHeight:设置受影响组件横向,纵向跨越的网格数。
fill:设置受影响组件如何占领空白区域。
weightx,weighty:设置受影响组件占领对于空间所增加的比重,默认为0;
代码示例:
package com.test;

import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.TextField;


public class GridBagLayoutDemo2 extends Frame {
	
	GridBagLayout gb =new GridBagLayout();
	GridBagConstraints gbc =new GridBagConstraints();
	
	public void init()
	{
		this.setLayout(gb);
		Label l1 = new Label("用户名:");
		Label l2 = new Label("密    码:");
		TextField naem = new TextField(20);
		TextField naem2 = new TextField(20);
		gbc.fill= GridBagConstraints.BOTH;
		gbc.gridwidth=1;
		
		gb.setConstraints(l1, gbc);
		this.add(l1);
		gbc.gridwidth=GridBagConstraints.REMAINDER;
		//gbc.gridwidth=3;
		gb.setConstraints(naem, gbc);
		this.add(naem);
		gbc.gridwidth=1;
		gb.setConstraints(l2, gbc);
		this.add(l2);
		gbc.gridwidth=GridBagConstraints.REMAINDER;
		//gbc.gridwidth=3;
		gb.setConstraints(naem2, gbc);
		this.add(naem2);
		
		this.setVisible(true);
		
		
		
		
		
	}
	
	
	
	public static void main(String[] args) {
		
		new GridBagLayoutDemo2().init();

	}

}

CardLayout

以时间而非空间来管理组件。它将所加入的组件看成是一叠卡片,只有最上面的那张才可见。