过程:
组件:
1个窗口
4个面板
10个button组件
第一步:面板3按照两行一列的表格布局添加按钮2与按钮3
第二步:面板4按照两行两列的表格布局顺序添加按钮6 7 8 9
第三步:面板1按照一行三列的表格布局顺序添加按钮1,面板3,按钮4
第四步:面板2按照一行三列的表格布局顺序添加按钮5,面板4,按钮10
第五步:窗口按照两行一列的表格布局顺序添加面板1,面板2
package com.zeng.GUI;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Text {
public static void main(String[] args) {
//十个组件
Button button1 = new Button("Button1");
Button button2 = new Button("Button2");
Button button3 = new Button("Button3");
Button button4 = new Button("Button4");
Button button5 = new Button("Button5");
Button button6 = new Button("Button6");
Button button7 = new Button("Button7");
Button button8 = new Button("Button8");
Button button9 = new Button("Button9");
Button button10 = new Button("Button10");
//窗口
Frame frame = new Frame("课堂测试");
frame.setSize(1500, 1500);
frame.setVisible(true);
//四个面板
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Panel panel3 = new Panel();
Panel panel4 = new Panel();
panel3.setLayout(new GridLayout(2, 1));//面板三设置1行两列的表格布局
panel3.add(button2);
panel3.add(button3);
//组装上半部分
panel1.setLayout(new GridLayout(1, 3));//面板1设置一行三列的表格布局
panel1.add(button1);
panel1.add(panel3);
panel1.add(button4);
panel4.setLayout((new GridLayout(2, 2)));//面板4设置为2行两列的表格布局
panel4.add(button6);
panel4.add(button7);
panel4.add(button8);
panel4.add(button9);
panel2.setLayout(new GridLayout(1, 3));//面板2设置为一行三列的表格布局
//组装下半部分
panel2.add(button5);
panel2.add(panel4);
panel2.add(button10);
//将组装好的面板1和面板2组装到窗口上
frame.setLayout(new GridLayout(2, 1));//窗口设置为两行一列的表格布局
frame.add(panel1);
frame.add(panel2);
//关闭窗口事件监听
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
}
}