博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5种常见的布局管理器
阅读量:2085 次
发布时间:2019-04-29

本文共 2957 字,大约阅读时间需要 9 分钟。

1、FlowLayout------像水流一样依次进行排列

 

import java.awt.*;import javax.swing.*;public class TestFlowLayout {	public static void main(String[] args) {		JFrame frame = new JFrame("Hello");// 实例化窗体对象		frame.setLayout(new FlowLayout(FlowLayout.CENTER,3,3));//所有组件居中对齐,水平和垂直间距为3		JButton button = null;		for(int i=0; i<9; i++){			button = new JButton("按钮 - "+i);			frame.add(button);//加入按钮		}		frame.setSize(280,140);//设置窗体大小		frame.setVisible(true);//设置窗体可见	}}

2、BorderLayout------分东南西北中五个区域

 

import java.awt.*;import javax.swing.*;public class TestBorderLayout {	public static void main(String[] args) {		JFrame frame = new JFrame("Hello");// 实例化窗体对象		frame.setLayout(new BorderLayout(3, 3));// 水平和垂直间距都是3		frame.add(new JButton("East"), BorderLayout.EAST);		frame.add(new JButton("South"), BorderLayout.SOUTH);		frame.add(new JButton("West"), BorderLayout.WEST);		frame.add(new JButton("North"), BorderLayout.NORTH);		frame.add(new JButton("Center"), BorderLayout.CENTER);		frame.pack();//根据组件自动调整窗体的大小		frame.setLocationRelativeTo(null);//居中		frame.setVisible(true);//设置窗体可见	}}

3、GridLayout------以表格形式进行排列,必须设置行数和列数

 

import java.awt.*;import javax.swing.*;public class TestGridLayout {	public static void main(String[] args) {		JFrame frame = new JFrame("Hello");// 实例化窗体对象		frame.setLayout(new GridLayout(4,4,3,3));//4*4进行排列,水平和垂直间距都是3		JButton button = null;		for(int i=0; i<13; i++){			button = new JButton("按钮 - "+i);			frame.add(button);//加入按钮		}		frame.pack();//根据组件自动调整窗体的大小		frame.setLocationRelativeTo(null);//居中		frame.setVisible(true);//设置窗体可见	}}

4、CardLayout------像一张张卡片一样,会彼此重叠,每次只展现一个界面

 

import java.awt.*;import javax.swing.*;public class TestCardLayout {	public static void main(String[] args) {		JFrame frame = new JFrame("Hello");// 实例化窗体对象		Container cont = frame.getContentPane();//取得窗体容器		CardLayout card = new CardLayout();		frame.setLayout(card);		cont.add(new JLabel("A",JLabel.CENTER),"first");		cont.add(new JLabel("B",JLabel.CENTER),"second");		cont.add(new JLabel("C",JLabel.CENTER),"third");		cont.add(new JLabel("D",JLabel.CENTER),"fourth");		cont.add(new JLabel("E",JLabel.CENTER),"fifth");		frame.pack();//根据组件自动调整窗体的大小		frame.setLocationRelativeTo(null);//居中		frame.setVisible(true);//设置窗体可见		card.show(cont, "third");//显示第三张卡片		for(int i=0; i<5; i++){			try{				Thread.sleep(1000);//加入显示延迟			} catch(Exception e) {				e.printStackTrace();			}			card.next(cont);//从容器中取出组件		}	}}

 

5、绝对定位------设置每一个组件的具体位置

 

import javax.swing.*;public class TestAbsoluteLayout {	public static void main(String[] args) {		JFrame frame = new JFrame("Hello");// 实例化窗体对象		frame.setLayout(null);//使用绝对定位		JLabel title = new JLabel("OK");		JButton enter = new JButton("Enter");		JButton help = new JButton("Help");		frame.setSize(200,100);		title.setBounds(45,5,150,20);		enter.setBounds(10,30,80,20);		help.setBounds(100,30,80,20);		frame.add(title);		frame.add(enter);		frame.add(help);		frame.setLocationRelativeTo(null);//居中		frame.setVisible(true);//设置窗体可见	}}

转载地址:http://lpsqf.baihongyu.com/

你可能感兴趣的文章
Jmeter之参数化
查看>>
Shell 和Python的区别。
查看>>
【JMeter】1.9上考试jmeter测试调试
查看>>
【虫师】【selenium】参数化
查看>>
【Python练习】文件引用用户名密码登录系统
查看>>
学习网站汇总
查看>>
【Loadrunner】性能测试报告实战
查看>>
【自动化测试】自动化测试需要了解的的一些事情。
查看>>
【selenium】selenium ide的安装过程
查看>>
【手机自动化测试】monkey测试
查看>>
【英语】软件开发常用英语词汇
查看>>
Fiddler 抓包工具总结
查看>>
【雅思】雅思需要购买和准备的学习资料
查看>>
【雅思】雅思写作作业(1)
查看>>
【雅思】【大作文】【审题作业】关于同不同意的审题作业(重点)
查看>>
【Loadrunner】通过loadrunner录制时候有事件但是白页无法出来登录页怎么办?
查看>>
【English】【托业】【四六级】写译高频词汇
查看>>
【托业】【新东方全真模拟】01~02-----P5~6
查看>>
【托业】【新东方全真模拟】03~04-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST05~06-----P5~6
查看>>