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

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

以时间而非空间来管理组件。它将所加入的组件看成是一叠卡片,只有最上面的那张才可见。
CardLayout常用五个方法:first,last,previous,next,show。
package com.test;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CardLayoutdemo extends Frame {
	
	CardLayout c  =new CardLayout();
	Panel pl =new Panel();
	
	public void init()
	{
		
		String[] names={"J","Q","K","A","小王","大王"};
		
		pl.setLayout(c);
		for(int i=0;i<names.length;i++)
		{
			Label l= new Label(""+names[i]);
			pl.add(l);
			
		}
		
		this.add(pl,BorderLayout.CENTER);
		
		
		
		Button pre= new Button("上一张");
		pre.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				c.previous(pl);
				
			}
			
		});
		
		Button las = new Button("下一张");
		
		las.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				c.next(pl);
				
			}
			
		});
		
		Panel p = new Panel();
		this.add(p,BorderLayout.SOUTH);
		
		p.add(pre);
		p.add(las);
		
		this.setVisible(true);
		
	}

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

BoxLayout

BoxLayout布局管理器常常与Box容器结合使用。这种布局管理器降低了GridBagLayout布局管理器的
难度。通过构造器的参数可以设置放入受影响容器的排列方式。Box对象是一种容器,但是不能独立存在,默认布管理器是BoxLayout。

空布局(NullLayout)

使用组件的setBounds方法可以再深以为位置放入组件,极大的提高了灵活性,操作简单,但是,可移植性不强。代码示例:
package com.test;

import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;

public class NullLayout extends Frame{
	
	
	public void init()
	{
		this.setLayout(null);
		Label l= new Label("登陆系统");
		Label l1 = new Label("用户名:");
		Label l2 = new Label("密    码:");
		TextField naem = new TextField(20);
		TextField naem2 = new TextField(20);
		Button reg= new Button("登陆");
		Button res= new Button("重置");
		l.setBounds(150,30,50,30);
		l1.setBounds(50, 60, 40, 40);
		l2.setBounds(50, 130, 40, 40);
		naem.setBounds(120, 60, 120, 40);
		naem2.setBounds(120, 130, 120, 40);
		reg.setBounds(100,250 , 40,40 );
		res.setBounds(200,250 , 40,40 );
		this.add(l);
		this.add(l2);
		this.add(l1);
		this.add(naem2);
		this.add(naem);
		this.add(res);
		this.add(reg);
		this.setSize(350,350);
		this.setResizable(false);
		this.setVisible(true);
	}
	
	

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

	}

}





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值