《化羽Java GUI学习笔记》 我自己的学习笔记以后方便复习,也希望可以给找不到方法学习的人一点点帮助。如果有错误请大家告诉一下相互学习。
除了创建Box实例的静态方法之外,Box类中还提供了一些创建不可见(invisible)组件的方法
例如:
public static Component createHorizontalGlue()
public static Component createVerticalGlue()
public static Component createHorizontalStrut(int width)
public static Component createVerticalStrut(int height)
public static Component createRigidArea(Dimension d)
运行结果:
Box 1 是没有添加不可见组件时的形式
Box 2,3和4是分别添加了不可见组件Glue,Strut和Rigid之后的形式,Glue将填满所有剩余
水平(或垂直)空间,Strut和Rigid则具有指定的宽度(或高度)
import java.awt.*;
import javax.swing.*;
public class GlueAndStrut {
private JFrame frame;
private Box b1,b2,b3,b4;
public static void main(String[] args) {
GlueAndStrut that = new GlueAndStrut();
that.go();
}
void go() {
frame = new JFrame("Glue and Strut example");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(4,1)); //4行1列
b1 = Box.createHorizontalBox();
b1.add(new JLabel("Box 1:"));
b1.add(new JButton("Yes"));
b1.add(new JButton("No"));
b1.add(new JButton("Cancel"));
b2 = Box.createHorizontalBox();
b2.add(new JLabel("Box 2:"));
b2.add(new JButton("Yes"));
b2.add(new JButton("No"));
b2.add(Box.createHorizontalGlue());
b2.add(new JButton("Cancel"));
b3 = Box.createHorizontalBox();
b3.add(new JLabel("Box 3:"));
b3.add(new JButton("Yes"));
b3.add(new JButton("No"));
b3.add(Box.createHorizontalStrut(20));
b3.add(new JButton("Cancel"));
b4 = Box.createHorizontalBox();
b4.add(new JLabel("Box 3:"));
b4.add(new JButton("Yes"));
b4.add(new JButton("No"));
b4.add(Box.createRigidArea(new Dimension(50,90)));
b4.add(new JButton("Cancel"));
contentPane.add(b1);
contentPane.add(b2);
contentPane.add(b3);
contentPane.add(b4);
frame.setSize(300,200);
frame.setVisible(true);
}
}