Example9_5.java
[sourcecode language=”java” title=”Example9_5.java”]
public class Example9_5 {
public static void main(String[] args) {
WindowBoxLayout win = new WindowBoxLayout(); //创建对象
win.setBounds(100,100,310,260); //设置窗口大小
win.setTitle("嵌套盒式布局容器"); //窗口标题
}
}
[/sourcecode]
WindowBoxLayout.java
[sourcecode language=”java” title=”WindowBoxLayout.java”]
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class WindowBoxLayout extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
Box baseBox,boxV1,boxV2; //创建盒式容器
public WindowBoxLayout() { //构造方法
setLayout(new java.awt.FlowLayout()); //布局方式
init(); //执行init方法
setVisible(true); //意思是说数据模型已经构造好了,允许JVM可以根据数据模型执行paint方法开始画图并显示到屏幕上了,并不是显示图形,而是可以运行开始画图了。
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//用户单击窗口的关闭按钮时程序执行的操作
}
void init() {
boxV1=Box.createVerticalBox(); //行型盒式布局的盒式容器
boxV1.add(new JLabel("姓名"));
boxV1.add(Box.createVerticalStrut(8)); //垂直支撑为8
boxV1.add(new JLabel("email"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(new JLabel("职业"));
boxV2=Box.createVerticalBox(); //列式盒式布局的盒式容器
boxV2.add(new JTextField(10)); //长度为10的文本框
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(new JTextField(10));
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(new JTextField(10));
baseBox=Box.createHorizontalBox(); //行型式盒式布局的盒式容器
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10)); //水平支撑为10
baseBox.add(boxV2);
add(baseBox);
}
}
[/sourcecode]