容器和版面配置

1,java awt

Container(容器):Window(Frame),Panel

Component(组件):Button,,,

Others:Layout,Color,,,

2,Container(容器)

1) Frame

public class FrameTest {

	
	public static void main(String[] args) {
		
		Frame f1 = new Frame();
		Frame f2 = new Frame();
		
		f1.setSize(100, 200);
		f2.setSize(200,100);
		f1.setVisible(true);
		f2.setVisible(false);

	}

}

2) Panel

import java.awt.*;
import java.applet.*;

public class AppletPanelTest extends Applet//注意Panel的实现方式
{
	public void init()
	{
		Panel p = new Panel();
		p.setBackground(Color.red);
		p.setSize(100,100);

		setBackground(Color.green);
		setSize(200,200);
		add(p);
	}
}

3,版面配置

1)BorderLayout(东西南北中)

        Frame f = new Frame("BorderLayout test");
        Button b1 = new Button("One");
        f.add(b1, BorderLayout.EAST);

2)FlowLayout(如果宽度够,自动放第一行,不够的话往第二行放)

3)CardLayout

4)GridLayout

    Frame f = new Frame("GridLayout test");

    f.setLayout(new GridLayout(2,3));//两行三列

5)GridBagLayout 设计你自己的版式

import java.awt.*;

public class GridBagLayoutStep1
{
	public static void main(String argv[])
	{
		Frame f = new Frame("GridBagLayout Step 1");
		f.setLayout(new GridBagLayout());

		Button b[] = new Button[4];
		int att[][] = { {0, 0, 1, 1}, 
						{1, 0, 1, 1}, 
						{2, 0, 1, 1},
						{0, 1, 3, 1} };

		for (int i=0; i<b.length; i++)
		{
			b[i] = new Button("Button "+(i+1));
	                add(f, b[i], att[i]);
		}

		f.pack();
		f.setVisible(true);
	}

	private static void add(Container con, Component com, int att[])//在容器con中按照限制要求的数组att来添加组件com
	{
		GridBagConstraints cons = new GridBagConstraints();//得到一个限制对象
		cons.gridx = att[0];
		cons.gridy = att[1];
		cons.gridwidth = att[2];
		cons.gridheight = att[3];

		con.add(com, cons);//在容器中按照限制cons添加组件com
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值