布局练习
作者:互联网
布局练习
package com.dong.lesson01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//练习的Dome讲解
public class ExDome {
public static void main(String[] args) {
//总 Frame
Frame frame = new Frame();
frame.setSize(400,300);
frame.setLocation(300,400);
frame.setLayout(new GridLayout(2,1));
frame.setBackground(Color.MAGENTA);
frame.setVisible(true);
//4个面板
Panel p1 = new Panel(new BorderLayout());
Panel p2 = new Panel(new GridLayout(2,1));
Panel p3 = new Panel(new BorderLayout());
Panel p4 = new Panel(new GridLayout(2,2));
//上面ok
p1.add(new Button("east-1"),BorderLayout.EAST);
p1.add(new Button("west-2"),BorderLayout.WEST);
p2.add(new Button("p2-btn-1"));
p2.add(new Button("p2-btn-2"));
p1.add(p2,BorderLayout.CENTER);
//下面
p3.add(new Button("east-2"),BorderLayout.EAST);
p3.add(new Button("west-3"),BorderLayout.WEST);
//中间4个
for (int i = 0; i < 4; i++) {
p4.add(new Button("for-"+i));
}
p3.add(p4,BorderLayout.CENTER);
frame.add(p1);
frame.add(p3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
总结:
-
Frame是一个顶级窗口
-
Panel无法单独显示,必须添加到某个容器中
-
布局管理器
- 流式 FlowLayout
- 东南西北中 BorderLayout
- 表格 GridLayout
-
大小、定位、背景颜色、可见性、监听
标签:练习,BorderLayout,frame,布局,add,new,Button,Panel 来源: https://blog.csdn.net/ljyyds56/article/details/120814750