Swing布局管理器介绍
来源:互联网 发布:伊利网络测评怎么算过 编辑:程序博客网 时间:2024/05/01 20:19
当选择使用JPanel和顶层容器的content pane时,需要考虑布局管理。JPanel缺省是初始化一个FlowLayout,而content pane缺省是初始化一个BorderLayout。
下面将分别介绍几种最常用的布局管理器:FlowLayout、BorderLayout、BoxLayout、CardLayout、GridLayout和GridBagLayout。
代码演示
每一个布局管理器都会有一个代码演示,xxxLayoutDemo.java(见附件)。这些文件主要有三个方法组成:
addComponentsToPane()提供布局逻辑(依据不同的布局管理器和UI内容)。
public static void addComponentsToPane(Container pane) {。。。}
createAndShowGUI()实例化一个JFrame,通过它的ContentPane加载布局逻辑内容。
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("FlowLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set up the content pane.
addComponentsToPane(frame.getContentPane());
// Display the window.
frame.pack();
frame.setVisible(true);
}
main()程序入口,单独起一个线程,实例化UI。
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
FlowLayout
FlowLayout类是最简单的布局管理器。它按照和页面上排列单词的类似方式来安排组件----从左到右,直至没有多余的空间,然后转到下一行。
效果:
内容面板代码:
public static void addComponentsToPane(Container pane) {
pane.setLayout(new FlowLayout());
pane.add(new JButton("Button 1"));
pane.add(new JButton("Button 2"));
pane.add(new JButton("Button 3"));
pane.add(new JButton("Long-Named Button 4"));
pane.add(new JButton("5"));
}
BorderLayout
一个BorderLayout对象将界面分成五大区域,分别用BorderLayout类的静态常量指定:
-PAGE_START
-PAGE_END
-LINE_START
-LINE_END
-CENTER
效果:
内容面板代码:
public static void addComponentsToPane(Container pane) {
JButton button = new JButton("Button 1 (PAGE_START)");
pane.add(button, BorderLayout.PAGE_START);
button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);
}
BoxLayout
BoxLayout可以将组件由上至下或由左至右依次加入当前面板。
效果:
内容面板代码:
public static void addComponentsToPane(Container pane) {
JPanel xPanel = new JPanel();
xPanel.setLayout(new BoxLayout(xPanel, BoxLayout.
Swing布局管理器介绍
Swing布局管理器介绍
Swing布局管理器介绍
Swing布局管理器介绍
Swing布局管理器介绍
Swing布局管理器介绍
Swing布局管理器介绍
Swing布局管理器
swing 布局管理器 GridBagLayout
Java Swing 布局管理器
Swing之布局管理器
Swing布局管理器
swing布局管理器总结
Swing 自定义布局管理器
Swing的布局管理器
Swing布局管理器
Swing的布局管理器
swing 布局管理器
win7小工具之凭据管理器
我的博客
平衡二叉树 详解
HowTo JTable or JXTable
Linux 线程模型的比较:LinuxThreads 和 NPTL
Swing布局管理器介绍
I'm here
用惯了VisualStudio的goto definition, 今天才知道有ctags这玩意儿,挺好用的!
不做毫无风险的项目
冷咖啡—断章
SwingUtilities中invokeLater和invokeAndWait
毕业~!
linux下挂载(mount)光盘镜像文件、移动硬盘、U盘、Windows和NFS网络共享
[NOI 2005]聪聪和可可(DP)
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
书法学习心得
钢笔书法图片
书法作品欣赏行书
春联毛笔书法作品欣赏
学书法有前途吗
米芾书法欣赏
中国书法大全
春联书法欣赏
行楷书法欣赏
书法网站大全
中国第一书法网
中国书法家园网
中国书法网作品欣赏
田蕴章行书书法欣赏
楷书书法欣赏
中国书法在线网
中国书法网首页
书法图片欣赏
王羲之书法欣赏
书法作品欣赏楷书
颜真卿书法欣赏
四字书法图片
书法欣赏高清图片
行书书法大全
钢笔书法学习
书法字体在线转换
书法字体下载
少儿书法培训班
怎么练毛笔书法
专业书法培训
硬笔书法 培训
老年书法培训班
毛笔书法学习
毛笔书法培训班
毛笔书法培训
毛笔书法考级
少儿硬笔书法班
隶书书法入门
书法自学入门
硬笔书法学习
名家书法作品图片