GridLayout布局管理器是以表格形式进行管理的,在使用此布局管理器时必须设置显示的行数和列数,GridLayout类的构造方法如表11.9所示。
构造方法声明 | 功能描述 |
---|---|
GridLayout() | 构造一个具有默认值的GridLaout布局管理器,即每个组件占一行一列 |
GridLayout(int r,int c) | 构造一个指定行和列数的GridLaout布局管理器 |
GridLayout(int rows,int cols,int hgap,int vgap) | 构造一个指定行和列数以及水平和垂直间距的GridLaout布局管理器 |
表12.19中列举了GridLayout类的构造方法,接下来通过一个案例来演示GridLayout布局管理器的使用,如例12-18所示。
例12-18 Demo1218.java
1import java.awt.*;
2import javax.swing.*;
3import java.awt.*;
4public class Demo1218 {
5 public static void main(String[] args) {
6 // 创建Frame对象
7 JFrame jFrame = new JFrame("Frame窗口下的网格式布局管理器");
8 // 设置窗体中布局管理器为GridLayout
9 jFrame.setLayout(new GridLayout(3,3,10,10));
10 JButton b = null;
11 for (int i = 1; i <=9; i++) {
12 b = new JButton(i+""); // 创建按钮
13 jFrame.add(b); // 将按钮加入到Frame
14 }
15 jFrame.setSize(400, 250); // 设置长和宽
16 jFrame.setLocation(500, 200); // 设置窗口相对位置
17 jFrame.setVisible(true); // 设置为可见
18 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19 }
20}
程序的运行结果如图12.20所示。
图12.20中,运行程序创建Frame窗体后,将布局设置为使用GridLayout布局管理器,设置以两行三列布局,并设置组件之间的水平和垂直间距都为10,之后添加6个按钮到Frame中,最后设置Frame窗体的长宽以及可见。
GridBagLayout类是在GridLayout类基础上提供的更为复杂的布局管理器。与GridLayout布局管理器不同的是,GridBagLayout类允许容器中各个组件的大小不相同,还允许单个组件所在的显示区域占多个网格。
使用GridBagLayout布局管理器的关键在于GridBagConstraints对象,在这个对象中设置相关属性,然后调用GridBagLayout对象的setConstraints()方法建立对象和受控组件直接的关联,GridBagConstraints类的常用属性如表12.20所示。
属性声明 | 功能描述 |
---|---|
gridx和gridy | 设置组件的左上角所在网格的横向和纵向索引(即所在的行和列) |
gridwidth和gridheight | 设置组件横向、纵向跨越几个网格,两个属性的默认值都是1 |
fill | 如果组件的显示区域大于组件需要的大小,设置是否以及如何改变组件大小 |
weightx和weighty | 设置组件占领容器中多余的水平方向和垂直方向空白的比例(也称为权重) |
表12.20中列举了GridBagConstraints类的常用属性,其中gridx和gridy的值如果设置为RELATIVE,表示当前组件紧跟在上一个组件后面;gridwidth和gridheight的值如果设为REMAINER,表示当前组件在其行或列上为最后一个组件,如果两个属性值都设为RELATIVE,表示当前组件在其行或列上为倒数第二个组件;weightx和weighty的默认值是0,例如容器中有2个组件,weightx分别为2和1,当容器宽度增加30个像素时,两个容器分别增加20和10个像素;fill属性可以接收4个属性值,具体示例如下:
NONE:默认,不改变组件大小
HORIZONTAL:使组件水平方向足够长以填充显示区域,但是高度不变
VERTICAL:使组件垂直方向足够高以填充显示区域,但长度不变
BOTH:使组件足够大,以填充整个显示区域
接下来通过一个案例来演示GridBagLayout布局管理器的使用,如例12-19所示。
例12-19 Demo1219.java
1import javax.swing.*;
2import java.awt.*;
3public class Demo1219 {
4 //向JFrame中添加JButton按钮
5 public static void makeButton(String title, JFrame jFrame, GridBagLayout gridBagLayout, GridBagConstraints constraints) {
6 JButton jButton = new JButton(title); //创建JButton对象
7 gridBagLayout.setConstraints(jButton, constraints);
8 jFrame.add(jButton);
9 }
10
11 public static void main(String[] agrs) {
12 JFrame frame = new JFrame("JFrame窗口下的网格包布局管理器");
13 //创建GridBagLayout布局管理器
14 GridBagLayout gbaglayout = new GridBagLayout();
15 GridBagConstraints constraints = new GridBagConstraints();
16 frame.setLayout(gbaglayout); //使用GridBagLayout布局管理器
17 constraints.fill = GridBagConstraints.BOTH; //组件填充显示区域
18 constraints.weightx = 0.0; //恢复默认值
19 constraints.gridwidth = GridBagConstraints.REMAINDER; //结束行
20 JTextField tf = new JTextField("13612345678");
21 gbaglayout.setConstraints(tf, constraints);
22 frame.add(tf);
23 constraints.weightx = 0.5; // 指定组件的分配区域
24 constraints.weighty = 0.2;
25 constraints.gridwidth = 1;
26 makeButton("7", frame, gbaglayout, constraints);
27 makeButton("8", frame, gbaglayout, constraints);
28 constraints.gridwidth = GridBagConstraints.REMAINDER; //结束行
29 makeButton("9", frame, gbaglayout, constraints);
30 constraints.gridwidth = 1; //重新设置gridwidth的值
31
32 makeButton("4", frame, gbaglayout, constraints);
33 makeButton("5", frame, gbaglayout, constraints);
34 constraints.gridwidth = GridBagConstraints.REMAINDER;
35 makeButton("6", frame, gbaglayout, constraints);
36 constraints.gridwidth = 1;
37
38 makeButton("1", frame, gbaglayout, constraints);
39 makeButton("2", frame, gbaglayout, constraints);
40 constraints.gridwidth = GridBagConstraints.REMAINDER;
41 makeButton("3", frame, gbaglayout, constraints);
42 constraints.gridwidth = 1;
43
44 makeButton("挂断", frame, gbaglayout, constraints);
45 constraints.gridwidth = GridBagConstraints.REMAINDER;
46 makeButton("拨号", frame, gbaglayout, constraints);
47 constraints.gridwidth = 1;
48 frame.setBounds(420, 420, 420, 420); //设置容器大小
49 frame.setVisible(true);
50 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
51 }
52}
程序的运行结果如图12.21所示。
图12.21中,运行程序创建Frame窗口后,创建了GridBagLayout对象和GridBagConstraints对象,调用makeButton() 方法向 JFrame窗口填充按钮,并使用 GridBagConstraints. REMAINDER 来控制结束行。当一行结束后,重新设置 GridBagConstraints 对象的 gridwidth 为 1,最后设置 JFrame 窗口为可见状态,并可关闭。
CardLayout布局管理器是将一些组件彼此重叠地进行布局,像一张张卡片叠放在一起一样,这样每次只会展现一个界面,CardLayout类的构造方法和常用方法如表12.21所示。
方法声明 | 功能描述 | |
---|---|---|
public CardLayout() | 创建一个各组件间距为0的CardLayout布局管理器 | |
public CardLayout(int hgap,int vgap) | 构造一个各组件指定水平和垂直间距的CardLayout布局管理器 | |
void next(Container parent) | 翻到下一张卡片 | |
void previous(Container parent) | 翻到上一张卡片 | |
void first(Container parent) | 翻到第一张卡片 | |
void last(Container parent) | 翻到最后一张卡片 | |
void show(Container parent,String name) | 显示具有指定组件名称的卡片 |
表12.21中列举了CardLayout类的构造方法和常用方法,接下来通过一个案例来演示CardLayout布局管理器的使用,如例12-20所示。
例12-20 Demo1220.java
1import java.awt.*;
2import javax.swing.*;
3
4public class Demo1220 extends JFrame {
5 public static void main(String[] args) {
6 //创建JFrame对象和两个面板对象
7 JFrame jFrame=new JFrame("JFrame窗口下的卡片式布局管理器");
8 JPanel jPanel1=new JPanel();
9 JPanel jPanel2=new JPanel();
10
11 jFrame.setLayout(null);
12 //将面板对象jPanel2设置为1行4列的网格式布局
13 jPanel2.setLayout(new GridLayout(1,4));
14 CardLayout cardLayout=new CardLayout(5,10);
15 jPanel1.setLayout(cardLayout);
16 jFrame.setSize(350,370);
17 jFrame.setResizable(false);
18 jPanel1.setBounds(10,10,320,200);
19 jPanel2.setBounds(10,220,320,25);
20 jFrame.add(jPanel1);
21 jFrame.add(jPanel2);
22
23 JLabel jLabel1=new JLabel("首页",JLabel.CENTER);
24 JLabel jLable2=new JLabel("第二页",JLabel.CENTER);
25 JTextField jTextField=new JTextField("卡片式布局案例",20);
26 jPanel1.add(jLabel1,"cd1");
27 jPanel1.add(jLable2,"cd2");
28 jPanel1.add(jTextField,"t1");
29 cardLayout.show(jPanel1,"t1");
30
31 jPanel2.add(new JButton("首页"));
32 jPanel2.add(new JButton("上一页"));
33 jPanel2.add(new JButton("下一页"));
34 jPanel2.add(new JButton("末页"));
35
36 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
37 jFrame.setVisible(true);// 设置窗体可见
38 }
39
40}
程序的运行结果如图12.22所示。
图12.22中,运行程序弹出Frame窗口,创建一个窗口对象和两个面板对象jPanel1、jPanel2,将面板jPanel2设置成1行4列的网格式页面布局,将面板jPanel1的页面布局设置为卡片式。jFrame调用add()方法将两个面板添加到窗口jFrame中,jPanel1调用add()方法将三个对象添加到面板jPanel1中,将名字为jTextField的文本框显示在卡片式布局的页面上,将4个命令按钮分别添加到面板jPanel2中,然后将各组件显示在窗口中。