2.2.3 布局管理器
-
流式布局:就是从左到右排列
package GUI; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class TestFlowLayout { public static void main(String[] args) { Frame frame = new Frame(); //组件--按钮 Button button1 = new Button("button1"); Button button2 = new Button("button2"); Button button3 = new Button("button3"); //设置为流式布局 //frame.setLayout(new FlowLayout());//默认为居中 //frame.setLayout(new FlowLayout(FlowLayout.LEFT));//左边 frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//右边 frame.setSize(200,200); //把按钮添加上去 frame.add(button1); frame.add(button2); frame.add(button3); frame.setVisible(true); //设置窗口关闭监听事件 frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } }
-
东西南北中
package GUI; import java.awt.*; public class TestBorderLayout { public static void main(String[] args) { Frame frame = new Frame("TestBorderLayout"); Button east = new Button("East"); Button west = new Button("West"); Button south = new Button("South"); Button north = new Button("North"); Button center = new Button("Center"); frame.add(east,BorderLayout.EAST); frame.add(west,BorderLayout.WEST); frame.add(south,BorderLayout.SOUTH); frame.add(north,BorderLayout.NORTH); frame.add(center,BorderLayout.CENTER); frame.setSize(200,200); frame.setVisible(true); } }
-
表格布局
package GUI; import java.awt.*; public class TestGridLayout { public static void main(String[] args) { Frame frame = new Frame("TestGridLayout"); Button btn1 = new Button("btn1"); Button btn2 = new Button("btn2"); Button btn3 = new Button("btn3"); Button btn4 = new Button("btn4"); Button btn5 = new Button("btn5"); Button btn6 = new Button("btn6"); frame.setLayout(new GridLayout(3,2)); //它会自动填充 frame.add(btn1); frame.add(btn2); frame.add(btn3); frame.add(btn4); frame.add(btn5); frame.add(btn6); frame.pack();//Java函数,自动选择最优 frame.setSize(200,200); frame.setVisible(true); } }
-
练习:做出下面的窗口样式:
代码如下:
package GUI;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class lianxi1 {
public static void main(String[] args) {
Frame frame = new Frame("练习嵌套组件");
frame.setVisible(true);
//设置窗口及组件的大小
frame.setSize(400,400);
frame.setLocation(200,100);
frame.setBackground(Color.blue);
//布局为表格布局,两行一列
frame.setLayout(new GridLayout(2,1));
//创建上下两个面板来装
Panel panel1 = new Panel(new BorderLayout());
Panel panel2 = new Panel(new BorderLayout());
//对于上边的Panel1
//首先创建4个button
Button button1 = new Button("Button1");
Button button2 = new Button("Button2");
Button button3 = new Button("Button3");
Button button4 = new Button("Button4");
//流式布局,左,中,右
panel1.add(button1,BorderLayout.WEST);
panel1.add(button4,BorderLayout.EAST);
//中间的用Panel4面板填充 里面用表格布局,两行一列
Panel panel4 = new Panel(new GridLayout(2,1));
panel1.add(panel4,BorderLayout.CENTER);
panel4.add(button2);
panel4.add(button3);
//对于下边的Panel2,中间那部分再放一个面板来放表格布局
//中间用面板填充,表格布局,两行两列
Panel panel3 = new Panel(new GridLayout(2,2));
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");
//流式布局,左,中,右
panel2.add(button5,BorderLayout.WEST);
panel2.add(button10,BorderLayout.EAST);
panel2.add(panel3,BorderLayout.CENTER);
panel3.add(button6);
panel3.add(button7);
panel3.add(button8);
panel3.add(button9);
frame.add(panel1);
frame.add(panel2);
//窗口关闭监听事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
- 做出来的样子:
2.2.3.1 总结:
-
Frame是一个顶级窗口
-
Panel无法单独显示,必须添加到某个容器中
-
布局管理器:
-
**流式
-
东西南北中
-
表格**
-
-
大小,定位,背景颜色,可见性,监听