1.Frame的常用布局方式主要分为三种,流式布局、东西南北中布局以及列表布局;
2.流式布局FlowLayout,以一排的方式去布局,会根据窗口的大小自适应
3.东西南北中布局BorderLayout,以东西南北中的方式去布局,但是里面的组件大小都不能自定义
(3.例)
Panel panel1 = new Panel();
panel1.setLayout(new BorderLayout());
panel1.add(new Button("1"),BorderLayout.WEST);
4.表格布局GirdLayout,以表格的方式进行布局,将组件在布局中按加入的顺序排列
(4.例)
Panel panel4 = new Panel();
panel4.setLayout(new GridLayout(2,2));
for (int i = 6; i < 10; i++) {
panel4.add(new Button(""+i));
}
panel3.add(panel4,BorderLayout.CENTER);
package study01;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyFrameTest {
public static void main(String[] args) {
MyFrame frame = new MyFrame(300, 300, 600, 600, Color.BLUE);
// study01.MyPanel panel = new study01.MyPanel(50, 50, 200, 200, Color.BLACK);
frame.setLayout(new GridLayout(2,1));
//第一行第一个
Panel panel1 = new Panel();
panel1.setLayout(new BorderLayout());
panel1.add(new Button("1"),BorderLayout.WEST);
//第二个
Panel panel2 = new Panel();
panel2.setLayout(new GridLayout(2,1));
panel2.add(new Button("2"));
panel2.add(new Button("3"));
panel1.add(panel2, BorderLayout.CENTER);
//第三个
panel1.add(new Button("4"),BorderLayout.EAST);
frame.add(panel1);
//第二行第一个
Panel panel3 = new Panel();
panel3.setLayout(new BorderLayout());
panel3.add(new Button("5"),BorderLayout.WEST);
//第二个
Panel panel4 = new Panel();
panel4.setLayout(new GridLayout(2,2));
for (int i = 6; i < 10; i++) {
panel4.add(new Button(""+i));
}
panel3.add(panel4,BorderLayout.CENTER);
//第三个
panel3.add(new Button("10"),BorderLayout.EAST);
frame.add(panel3);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}