布局管理器练习

布局管理器练习

 

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Test01 extends Frame {
public static void main(String[] args) {
new Test01();
}

public Test01() {
this.setLayout(new GridLayout(2,1));

//窗口参数
this.setSize(300,400);
this.setLocation(400,300);
this.setBackground(Color.black);
this.setVisible(true);

//创建面板
Panel panel1 = new Panel(new BorderLayout());
Panel panel2 = new Panel(new GridLayout(2,1));
Panel panel3 = new Panel(new BorderLayout());
Panel panel4 = new Panel(new GridLayout(2,2));

//上面添加按钮
panel1.add(new Button("Button"),BorderLayout.EAST);
panel1.add(new Button("Button"),BorderLayout.WEST);
panel2.add(new Button("Button"));
panel2.add(new Button("Button"));
panel1.add(panel2,BorderLayout.CENTER);

//下面添加按钮
panel3.add(new Button("Button"),BorderLayout.EAST);
panel3.add(new Button("Button"),BorderLayout.WEST);
for (int i = 0; i < 4; i++) {
panel4.add(new Button("Button"));
}

//添加面板
panel3.add(panel4,BorderLayout.CENTER);
this.add(panel1);
this.add(panel3);

//窗口关闭
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

}
}

 

运行截图

布局管理器练习

 

上一篇:常用布局管理器


下一篇:2021-05-11