您的位置:首页 > 其它

Swing框式布局(BoxLayout,Box)应用详解

2010-04-07 14:25 155 查看
转自 http://www.boxcode.cn/html/JAVA/J2SE/2009/0813/636.html

框式布局

利用无序网格布局,几乎可以产生任意复杂的布局模型。除了这一个AWT布局管理器之外,Swing还引入了另外一个叫做框式布局(Box Layout)的结构。框式布局能够生成复杂的布局结构,但却没有像使用无序网格布局那样麻烦。编程者不必处理任何权值,也不用采用反复试验的方法。使用框式布局,可以像使用流式布局一样简单地将组件安排在一个容器内。包含框式布局的容器可以嵌套使用,最终达到类似于无序网格布局那样的效果。

框式布局模式有两种类型:水平模式和垂直模式。水平模式把组件在容器内从左到右排列。垂直模式把组件在容器内从上到下进行排列。框式布局所取的水平方向和垂直方向分别由x轴和y轴表示。因此,对于一个给定的框式布局来说,x轴和y轴将成为主轴方向。

任何时候,当用框式布局把一个组件加到容器内时,该组件的大小将会保持不变。这就是说,框式布局与网格布局不同,它允许组件沿主轴方向占据不同的空间大小。

在非主轴方向上,如果几个组件大小不同,那么框式布局就会以尺寸最大的那个组件的大小来布置所有的组件。例如,在水平框式布局中,如果所有的组件的高度不同(非主轴方向上),则布局管理器就会力图使所有组件都与最高的组件具有同样的高度。

如果不能沿非主轴方向调整尺寸,则组件就会以该轴为中心线进行排列。也就是说,在非主轴方向上,任何一个组件的中心坐标与其他组件的中心坐标是一致的。

如果改变了容器的大小,框式布局不会把任何组件折到下一行去。例如,在一个包括一些按钮的水平框式布局中,改变当前容器的大小时,位于一行上的按钮将不会被折到下一行上。

BoxLayout类

BoxLayout类实现了产生于抽象窗口工具库(AWT)的LayoutManager2接口。该类是Swing库的成员,存放在javax.swing包中。除java.lang.Object以外,这个类不作为其他任何类的子类。

通常,不用BoxLayout类来处理框式布局,而是用下一节将要介绍的Box类。然而,BoxLayout类有几个重要特性需加以注意。

要创建框式布局来从上到下或者从左到右排列组件,可以使用下面的构造函数:

public BoxLayout(Container target, int axis)

这里,target是布局的容器,axis指定框式布局的方向为水平的或者垂直的,并取下列静态常数之一:BoxLayout.X_AXIS、BoxLayout.Y_AXIS

Box容器

Box类表示一个轻量级容器,它只能使用框式布局。试图把任何其他类型的布局管理器赋给它时,将会产生一个AWTError。Box容器允许组件根据指定的方向水平排列组件或者垂直排列。Box对象由取axis参数的构造函数或者用很方便的静态方法创建。下面是Box类的构造函数:

public Box(int axis)

参数axis取值为BoxLayout.X_AXIS或BoxLayout.Y_AXIS。下面是两个很方便的静态方法,它们无需任何参数就可分别创建水平的或垂直的方框容器:

public static Box createHorizontalBox()

public static Box createVerticalBox()

Box类还提供了有用的方法,这些方法增加了一些特性,如产生组件内部的间隔、当容器改变大小时将组件重新定位,等等。这些特性的实现都用到了诸如glues、fillers、struts、rigid区域这样的不可见组件(invisible components)。

glue组件可扩充到必要的大小,以便填满框式布局中相邻组件之间的空间。这就导致在水平组件中会产生最大宽度,在垂直布局中产生最大高度。利用glues组件,可以使组件与组件之间以及组件与容器之间的额外空间得到平均分布。可以用一下两个静态方法分别创建针对水平布局和垂直布局的glue组件:

public static Component createHorizontalGlue()

public static Component createVerticalGlue()

strut大小是固定的。可以用这个组件向框式布局内的两个组件之间加入固定数量的间隔。例如,在一个水平布局中,这个组件就可以子两个组件之间加入一定的间隔。strut也可以用来使方框本身具有一定的大小。例如,在一个垂直放置的方框中,strut可用来使方框的宽度独有某个特定的值。strut的尺寸在没有指定的方向上基本上是无限制的。所以,对水平方向的方框来说,其高度就没有限制。

rigid区域占有一定的尺寸。rigid区域组件的功能类似于struts组件。可以指定该组件的宽度和高度。下面是创建rigid区域组件的静态方法:

public static Component createRigidArea(Dimension d)

fillers组件允许指定尺寸的最小值,最大值以及某个期望的值。过滤器是一些内部类Box.Filler对象。当把一个过滤器添加到水平方框中的两个组件之间时,它就会保持最小的宽度值,并且保证容器的高度最小。可以用以下所示的方法创建过滤器对象:

Box.Filler(new Dimension(w1, h1), //mininum size
new Dimension(w2, h2), //prefered size
new Dimension(w3, h3))//maxnum size

BoxLayout代码示例(利用不可见的组件放置其他组件)

下面的程序清单用框式布局创建了一个小程序,其中分别在不同的窗格里显示了几对按钮:B1和B2,B3和B4,等等,以此演示像glue、filler、rigid以及strut这样的不可见组件的效果。

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

public class TBox1 extends JApplet {
Container container = null;

public void init() {
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(lookAndFeel);
}catch(Exception e) {
e.printStackTrace();
}

//1.Get the handle on the applet's content pane.
container = getContentPane();

//2.Create a vertical box and add it to the applet.
Box basebox = Box.createVerticalBox();
container.add(basebox);

//3.Create the demo panel1 with box layout to display
//buttons B1 and B2.
//a)Create the panel and set the box layout.
JPanel glueDemoPanel1 = new JPanel();
glueDemoPanel1.setLayout(new BoxLayout(glueDemoPanel1, BoxLayout.X_AXIS));
//b)Set the title border around the panel.
TitledBorder border1 = new TitledBorder(
new LineBorder(Color.black), "Glue Component: Demo-1");
border1.setTitleColor(Color.black);
glueDemoPanel1.setBorder(border1);
//c)Add the buttons B1 and B2 with a glue component between them.
glueDemoPanel1.add(new JButton("B1"));
glueDemoPanel1.add(Box.createHorizontalGlue());
glueDemoPanel1.add(new JButton("B2"));
//d)Add the panel to the base box.
basebox.add(glueDemoPanel1);

//4.Create the demo panel-2, assign box layout and add
//buttons B3 and B4.
//a)Create the glue panel 2 and assign the box layout.
JPanel glueDemoPanel2 = new JPanel();
glueDemoPanel2.setLayout(new BoxLayout(glueDemoPanel2, BoxLayout.X_AXIS));
//b)Add a titled border to the panel.
TitledBorder border2 = new TitledBorder(new LineBorder(Color.black),"Glue Component: Demo-2");
glueDemoPanel2.setBorder(border2);
//c)Add the buttons B3 and B4 to the panel; also add
//the glue components between the buttons, and the
//buttons and panel.
glueDemoPanel2.add(Box.createHorizontalGlue());
glueDemoPanel2.add(new JButton("B3"));
glueDemoPanel2.add(Box.createHorizontalGlue());
glueDemoPanel2.add(new JButton("B4"));
glueDemoPanel2.add(Box.createHorizontalGlue());
//d)Add the panel to the base box.
basebox.add(glueDemoPanel2);

//5.Create a filler panel and add buttons B5 and B6.
//a)Create the panel object and assign box layout.
JPanel fillerDemoPanel = new JPanel();
fillerDemoPanel.setLayout(new BoxLayout(fillerDemoPanel, BoxLayout.X_AXIS));
//b)Set the titled border to the above panel.
TitledBorder border3 = new TitledBorder(
new LineBorder(Color.black),
"Filler Component Demo");
border3.setTitleColor(Color.black);
fillerDemoPanel.setBorder(border3);
//c)Add buttons B5 and B6 to the panel; also add the
//filler component between the buttons.
fillerDemoPanel.add(new JButton("B5"));
fillerDemoPanel.add(new Box.Filler(
new Dimension(50, 75),
new Dimension(50, 75),
new Dimension(Short.MAX_VALUE, 75)));
fillerDemoPanel.add(new JButton("B6"));
//d)Attach the panel to the base box.
basebox.add(fillerDemoPanel);

//6.Create rigid area and add buttons B7 and B8.
//a)Create a panel and assign the box layout.
JPanel rigidDemoPanel = new JPanel();
rigidDemoPanel.setLayout(new BoxLayout(rigidDemoPanel, BoxLayout.X_AXIS));
//b)Assign the title border around the panel.
TitledBorder border4 = new TitledBorder(
new LineBorder(Color.black),
"Rigid Area Component Demo");
border4.setTitleColor(Color.black);
rigidDemoPanel.setBorder(border4);
//c)Add buttons B7 and B8 to the rigid area panel.
//Also add a rigid area in the middle of the buttons.
rigidDemoPanel.add(new JButton("B7"));
rigidDemoPanel.add(Box.createRigidArea(new Dimension(150, 0)));
rigidDemoPanel.add(new JButton("B8"));
//d)Add the panel to the base box.
basebox.add(rigidDemoPanel);

//7.Create the strut panel, assign the box layout, and add
//the buttons B9 and B10.
//a)Create the panel and assign the box layout
JPanel strutDemoPanel = new JPanel();
strutDemoPanel.setLayout(new BoxLayout(strutDemoPanel, BoxLayout.X_AXIS));
//b)Set the titled border around the pane.
TitledBorder border5 = new TitledBorder(
new LineBorder(Color.black),
"Strut Component Demo");
border5.setTitleColor(Color.black);
//c)Add buttons B9  and B10 to the panel. Also assign the
//horizontal strut in the middle of the buttons.
strutDemoPanel.setBorder(border5);
strutDemoPanel.add(new JButton("B9"));
strutDemoPanel.add(Box.createHorizontalStrut(150));
strutDemoPanel.add(new JButton("B10"));
//d)Add the panel to the base box.
basebox.add(strutDemoPanel);
}
}

运行结果:



重叠布局:

import javax.swing.*;
import java.awt.*;

public class TBox2 extends JApplet {
Container container = null;

public void init() {
//1.Get the handle on the applet's content pane.
//and set the background color to white.
container = getContentPane();
container.setBackground(Color.white);

//2.Create a horizontal box and add it to the applet.
Box baseBox = Box.createHorizontalBox();
container.add(baseBox);

//3.Create a vertical box and add it to the base box.
Box vBox = Box.createVerticalBox();
baseBox.add(vBox);

//4.Create button B1 and add it to vBox.
JButton b1 = new JButton("B1");
b1.setAlignmentX(Component.CENTER_ALIGNMENT);
b1.setMaximumSize(new Dimension(150, 150));
vBox.add(b1);

//5.Create a horizontal box and add it to vBox.
Box hBox = Box.createHorizontalBox();
vBox.add(hBox);

//6.Create button B2 and add it to hBox.
JButton b2 = new JButton("B2");
b2.setAlignmentY(Component.CENTER_ALIGNMENT);
b2.setMaximumSize(new Dimension(75, 100));
hBox.add(b2);

//7.Create another button B3 and add it to hBox.
JButton b3 = new JButton("B3");
b3.setAlignmentY(Component.CENTER_ALIGNMENT);
b3.setMaximumSize(new Dimension(75, 100));
hBox.add(b3);

//8.Create the vertical box (vBox1) and add it to the base box.
Box vBox1 = Box.createVerticalBox();
baseBox.add(vBox1);

//9.Create the button B4 and add it to vBox1.
JButton b4 = new JButton("B4");
b4.setAlignmentX(Component.CENTER_ALIGNMENT);
b4.setMaximumSize(new Dimension(100, 250));
vBox.add(b4);
}
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: