布局管理器
用来管理组件在容器中的布局格式,位于 java.awt 包中。
常用的布局管理器有五种:
- FlowLayout (流布局)
- BorderLayout (边界布局)
- GridLayout (网格布局)
- CardLayout (卡片布局)
- NullLayout(null布局)
一、FlowLayout (流布局)
特点: 组件按照 “从左到右” 的顺序流动的安排到容器中,直到占满上方的空间,则移动到下一行,继续流动。
代码实现:
package ch10;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FlowLayoutDemo extends JFrame{
private JPanel jPanel;
private JButton b1Button;
private JButton b2Button;
private JButton b3Button;
//构造方法
public FlowLayoutDemo() {
super("流布局");
//创建面板对象
jPanel = new JPanel();
//创建按钮对象
b1Button = new JButton("yes");
b2Button = new JButton("or");
b3Button = new JButton("no");
//声明一个布局对象
FlowLayout f = new FlowLayout(FlowLayout.LEFT,10,15);
//设置面板的布局
jPanel.setLayout(f);
//将按钮添加到面板中
jPanel.add(b1Button);
jPanel.add(b2Button);
jPanel.add(b3Button);
//将面板添加到窗体中
this.add(jPanel);
//设置窗体属性
this.setSize(500,500);
this.setLocation(300,300);
}
public static void main(String[] args) {
FlowLayoutDemo fldDemo = new FlowLayoutDemo();
fldDemo.setVisible(true);
}
}
二、BorderLayout (边界布局)
特点: 该布局允许将组件有选择的放置到容器的中、北、南、东、西部,并且是窗体框架JFrame的默认布局。
package ch10;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BorderLayoutDemo extends JFrame{
private JPanel jPanel;
private JButton b1Button,b2Button,b3Button,b4Button,b5Button;
public BorderLayoutDemo() {
super("边界布局");
jPanel = new JPanel();
b1Button = new JButton("东");
b2Button = new JButton("西");
b3Button = new JButton("南");
b4Button = new JButton("北");
b5Button = new JButton("中");
//将布局设置到面板中
/*
BorderLayout bL = new BorderLayout();
jPanel.setLayout(bL);
*/
jPanel.setLayout(new BorderLayout());
//将按钮放置到面板指定位置
jPanel.add(b1Button,BorderLayout.EAST);
jPanel.add(b2Button,BorderLayout.WEST);
jPanel.add(b3Button,BorderLayout.SOUTH);
jPanel.add(b4Button,BorderLayout.NORTH);
jPanel.add(b5Button,BorderLayout.CENTER);
this.add(jPanel);
this.setSize(500,500);
this.setLocation(100,100);
//this.方法名 表示使用本类方法
}
public static void main(String[] args) {
BorderLayoutDemo bLayoutDemo = new BorderLayoutDemo();
bLayoutDemo.setVisible(true);
}
}
三、 GridLayout (网格布局)
特点: 像表格一样,按行和列排列所有组件,且每个单元大小都一样,添加组件时,他们将按照从左到右,从上到下的顺序加入。
package ch10;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridLayoutDemo extends JFrame{
private JPanel jPanel;
private JButton b1Button,b2Button,b3Button,b4Button;
public GridLayoutDemo() {
super("网格布局");
//调用有参JPanel的构造方法,直接创建一个特定布局的面板
jPanel = new JPanel(new GridLayout(2,2));
b1Button = new JButton("button1");
b2Button = new JButton("button2");
b3Button = new JButton("button3");
b4Button = new JButton("button4");
jPanel.add(b1Button);
jPanel.add(b2Button);
jPanel.add(b3Button);
jPanel.add(b4Button);
this.add(jPanel);
this.setSize(300,200);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
GridLayoutDemo gDemo = new GridLayoutDemo();
gDemo.setVisible(true);
}
}
四、 CardLayout (卡片布局)
特点: 将加入的组件像卡片一样叠在一起,只能看到最上面的组件,可以调用CardLayout的一些方法,显示特定的组件。
package ch10;
import java.awt.CardLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardLayoutDemo extends JFrame {
private JPanel jPanel;
private JButton b1Button,b2Button,b3Button;
//创建卡片布局对象
private CardLayout cLayout;
public CardLayoutDemo() {
super("卡片布局");
//调用JPanel的有参构造方法,构造指定布局的面板
cLayout = new CardLayout();
jPanel = new JPanel(cLayout);
b1Button = new JButton("王祺灏");
b2Button = new JButton("刘琦");
b3Button = new JButton("张浩卿");
//组件添加到面板
jPanel.add(b1Button,"1");
jPanel.add(b2Button,"2");
jPanel.add(b3Button,"3");
//显示第二张卡片
cLayout.show(jPanel,"2");
this.add(jPanel);
this.setSize(200,150);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
CardLayoutDemo cDemo = new CardLayoutDemo();
cDemo.setVisible(true);
}
}
五、NullLayout(null布局)
特点: null布局就是不采用任何布局,可以通过设置组件在容器中的位置及大小来安排位置。
注意: null布局一般用在组件位置相对固定,而且窗口不允许随意变换大小的情况。 (组件的相对位置会因为窗口大小发生变化时而发生改变。)
package ch10;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class NullLayoutDemo extends JFrame {
private JPanel jPanel;
private JButton b1Button,b2Button;
public NullLayoutDemo() {
super("空布局");
jPanel = new JPanel();
//设置面板布局为空
jPanel.setLayout(null);
b1Button = new JButton("确认");
b2Button = new JButton("取消");
//设置按钮大小
b1Button.setBounds(30,60,60,25);
b2Button.setBounds(100,60,60,25);
//将按钮添加到面板
jPanel.add(b1Button);
jPanel.add(b2Button);
//将面板添加到窗体
this.add(jPanel);
this.setSize(200,150);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
NullLayoutDemo nDemo = new NullLayoutDemo();
nDemo.setVisible(true);
}
}