《Java编程思想》读书笔记(6)
import java.awt.*;
import javax.swing.*;
public class Test extends JApplet
{
private JButton btn1;
private JButton btn2;
/**
* @param args
*/
public void init()
{
this.btn1 = new JButton("确定");
this.btn2 = new JButton("重置");
Container con = this.getContentPane();
con.setLayout(new FlowLayout());
con.add(btn1);
con.add(btn2);
}
public static void main(String[] args)
{
Test t1 = new Test();
Console.run(t1,300,300);
}
}
这一节没有过多的东西可以思考,但布局管理器的知识值得去深究。
Java语言中提供的布局管理器种类有:边界式布局、卡片式布局、流式布局和网格式布局等,各有不同的特点,可根据实际 需要选用;但有最大自由设计空间的是“无布局管理器”——即不使用任何布局格式,而通过手工方式添加组件到页面布局的绝对位置上。因此我采用“无布局管理器”将他的代码改写如下:
import java.awt.*;
import javax.swing.*;
public class Test extends JApplet
{
private JButton btn1;
private JButton btn2;
/**
* @param args
*/
public void init()
{
this.btn1 = new JButton("确定");
this.btn2 = new JButton("重置");
Container con = this.getContentPane();
//con.setLayout(new FlowLayout());
con.setLayout(null);
btn1.reshape(30,30,100,100);
btn2.reshape(150,150,100,100);
con.add(btn1);
con.add(btn2);
}
public static void main(String[] args)
{
Test t1 = new Test();
Console.run(t1,600,600);
}
}
1. GridLayout
GridLayout类是布局管理器之一,它将容器中的组件在放置在一个矩形网格中。容器被划分为相同大小的矩形,而每个组件就放置在一个单独的矩形中。
import javax.swing.*;
public class GridForm extends JFrame
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
GridForm gf = new GridForm();
gf.setLayout(new GridLayout(3,2));
Container con = gf.getContentPane();
con.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
con.add(new JButton("1"));
con.add(new JButton("2"));
con.add(new JButton("3"));
con.add(new JButton("4"));
con.add(new JButton("5"));
con.add(new JButton("6"));
gf.setTitle("GridLayout例子");
gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gf.setSize(200,200);
gf.setVisible(true);
}
}
如果容器的ComponentOrientation
属性是水平的并且从左到右,上面例子产生如下图1:
如果容器的ComponentOrientation属性是
horizontal 和 right-to-left,结果就如下图2:
当行数和列数都被设置为非零值(不管是通过构造函数或者通过setRows和setColumns方法来设置),指定的列数就会被忽略掉。例如说,如果指定是3行3列,而将布局中放置9个组件,它们就会显示为3行3列。
2.BorderLayout
边界布局会把容器看成五个:部分:north, south, east, west,和 center。每个区域最多包含一个组件,重要的五个常量:NORTH
, SOUTH
, EAST
, WEST
和 CENTER
;
p.setLayout(new BorderLayout());
p.add(new Button("Okay"), BorderLayout.SOUTH);
==================================================
==================================================
Panel p2 = new Panel();
p2.setLayout(new BorderLayout());
p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER);
BorderLayout
支持相对定位常量, PAGE_START
, PAGE_END
, LINE_START
, and LINE_END
. 在一个容器中,如果它的 ComponentOrientation
被设置为ComponentOrientation.LEFT_TO_RIGHT
,这些常量就分别映射为 NORTH
, SOUTH
, WEST
, and EAST
为了与以前兼容,BorderLayout
也好汉相对定位常量 BEFORE_FIRST_LINE
, AFTER_LAST_LINE
, BEFORE_LINE_BEGINS
and AFTER_LINE_ENDS
. 他们分别和PAGE_START
, PAGE_END
, LINE_START
and LINE_END
等价.
如果同时使用绝对定位和相对定位,就会产生不可预测的结果。例如,如果你在容器中同时使 NORTH
和 PAGE_START
常量 来添加组件,而容器的 方向是 LEFT_TO_RIGHT
, 那么就只有PAGE_START
被会布局。
import java.awt.*;
import javax.swing.*;
public class GridForm extends JFrame
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
GridForm gf = new GridForm();
gf.setLayout(new BorderLayout());
Container con = gf.getContentPane();
con.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
con.add(new JButton("North"),BorderLayout.NORTH);
con.add(new JButton("SOUTH"),BorderLayout.SOUTH);
con.add(new JButton("EAST"),BorderLayout.EAST);
con.add(new JButton("WEST"),BorderLayout.WEST);
con.add(new JButton("CENTER"),BorderLayout.CENTER);
gf.setTitle("BorderLayout例子");
gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gf.setSize(200,200);
gf.setVisible(true);
}
}
3. FlowLayout
FlowLayout会将组件从左到右进排列,就象文章中的文本行一样。流布局一般用在面板中的按纽的放置。它将会将按纽以从左至右的方式排放,直到在同一行中再也放不下更多的按纽了。
import javax.swing.*;
public class GridForm extends JFrame
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
GridForm gf = new GridForm();
//gf.setLayout(new BorderLayout());
gf.setLayout(new FlowLayout());
Container con = gf.getContentPane();
con.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
con.add(new JButton("North"));
con.add(new JButton("SOUTH"));
con.add(new JButton("EAST"));
con.add(new JButton("WEST"));
con.add(new JButton("CENTER"));
gf.setTitle("FlowLayout例子");
gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gf.setSize(200,200);
gf.setVisible(true);
}
}
4.BoxLayout
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
posted on 2006-06-01 23:25 Phinecos(洞庭散人) 阅读(1057) 评论(1) 编辑 收藏 举报