概要
边框布局管理器
边框布局管理器
注意
边框布局管理器并不要求所有区域都必须有组件,如果四周的区域(North、South、East 和 West 区域)没有组件,则由 Center 区域去补充。如果单个区域中添加的不只一个组件,那么后来添加的组件将覆盖原来的组件,所以,区域中只显示最后添加的一个组件
BorderLayout构造方法
BorderLayout类成员变量
Container方法
public Component add(String name, Component comp) {
addImpl(comp, name, -1);
return comp;
}
Demo01
import javax.swing.*;
import java.awt.*;
public class Demo09 {
public static void main(String[] args) {
//创建JFrame类对象
JFrame jFrame = new JFrame("边框父窗体");
//获取一个容器
Container container = jFrame.getContentPane();
//设置窗体大小
BorderLayout borderLayout = new BorderLayout();
//设置JFRame窗体的布局格式
jFrame.setLayout(borderLayout);
JButton button1 = new JButton("NORTH");
JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JButton button5 = new JButton("EAST");
container.add(BorderLayout.NORTH,button1);
container.add(BorderLayout.SOUTH,button2);
container.add(BorderLayout.CENTER,button3);
container.add(BorderLayout.WEST,button4);
container.add(BorderLayout.EAST,button5);
// jFrame.add(button1,BorderLayout.SOUTH);
jFrame.setSize(400,400);
//设置窗体可见
jFrame.setVisible(true);
//设置窗体关闭方式
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//创建边框布局管理器类对象
}
}
Demo01运行效果如下
import javax.swing.*;
import java.awt.*;
public class Demo09 {
public static void main(String[] args) {
//创建JFrame类对象
JFrame jFrame = new JFrame("边框父窗体");
//获取一个容器
Container container = jFrame.getContentPane();
//设置窗体大小
BorderLayout borderLayout = new BorderLayout();
//设置JFRame窗体的布局格式
jFrame.setLayout(borderLayout);
JButton button1 = new JButton("NORTH");
//JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JButton button5 = new JButton("EAST");
container.add(BorderLayout.NORTH,button1);
container.add(BorderLayout.SOUTH,button2);
container.add(BorderLayout.CENTER,button3);
container.add(BorderLayout.WEST,button4);
// container.add(BorderLayout.EAST,button5);
// jFrame.add(button1,BorderLayout.SOUTH);
jFrame.setSize(400,400);
//设置窗体可见
jFrame.setVisible(true);
//设置窗体关闭方式
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//创建边框布局管理器类对象
}
}
import javax.swing.*;
import java.awt.*;
public class Demo09 {
public static void main(String[] args) {
//创建JFrame类对象
JFrame jFrame = new JFrame("边框父窗体");
//获取一个容器
Container container = jFrame.getContentPane();
//设置窗体大小
BorderLayout borderLayout = new BorderLayout();
//设置JFRame窗体的布局格式
jFrame.setLayout(borderLayout);
JButton button1 = new JButton("NORTH");
JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JButton button5 = new JButton("EAST");
container.add(BorderLayout.NORTH,button1);
container.add(BorderLayout.SOUTH,button2);
container.add(BorderLayout.CENTER,button3);
container.add(BorderLayout.WEST,button4);
// container.add(BorderLayout.EAST,button5);
// jFrame.add(button1,BorderLayout.SOUTH);
jFrame.setSize(400,400);
//设置窗体可见
jFrame.setVisible(true);
//设置窗体关闭方式
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//创建边框布局管理器类对象
}
}