流布局:
package ch10;
import java.awt.FlowLayout;
import javax.swing.*;
public class FlowLayoutDemo extends JFrame{
private JPanel p;
private JButton b1,b2,b3;
public FlowLayoutDemo() {
super("流布局");
p=new JPanel();
FlowLayout layout = new FlowLayout(FlowLayout.LEFT,10,15);
//设置面板布局
//使用setLayout方法将布局对象设置到面板中
p.setLayout(layout);
b1 = new JButton("Button 1");
b2 = new JButton("Button 2");
b3 = new JButton("Button 3");
p.add(b1);
p.add(b2);
p.add(b3);
this.add(p);
this.setSize(200,150);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
FlowLayoutDemo layout = new FlowLayoutDemo();
layout.setVisible(true);
}
}
边界布局:
package ch10;
import java.awt.BorderLayout;
import javax.swing.*;
public class BorderLayoutDemo extends JFrame{
private JPanel p;
private JButton b1,b2,b3,b4,b5;
public BorderLayoutDemo() {
super("边界布局");
p = new JPanel();
//创建一个边界布局管理器对象,并将该布局设置到面板中
p.setLayout(new BorderLayout());
b1 = new JButton("Button 东");
b2 = new JButton("Button 西");
b3 = new JButton("Button 南");
b4 = new JButton("Button 北");
b5 = new JButton("Button 中");
//添加按钮
p.add(b1,BorderLayout.EAST);
p.add(b2,BorderLayout.WEST);
p.add(b3,BorderLayout.SOUTH);
p.add(b4,BorderLayout.NORTH);
p.add(b5,BorderLayout.CENTER);
this.add(p);
this.setSize(300,200);
this.setLocation(100, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
BorderLayoutDemo f = new BorderLayoutDemo();
f.setVisible(true);
}
}
网格布局:
package ch10;
import java.awt.GridLayout;
import javax.swing.*;
public class GridLayoutDemo extends JFrame{
private JPanel p;
private JButton b1,b2,b3,b4;
public GridLayoutDemo() {
super("网格布局");
p = new JPanel(new GridLayout(2,2));
b1 = new JButton("Button 1");
b2 = new JButton("Button 2");
b3 = new JButton("Button 3");
b4 = new JButton("Button 4");
//将按钮放到面板中
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
this.add(p);
this.setSize(300,200);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
GridLayoutDemo f = new GridLayoutDemo();
f.setVisible(true);
}
}
卡片布局:
package ch10;
import javax.swing.*;
import java.awt.*;
public class CardLayoutDemo extends JFrame{
private JPanel p;
private JButton b1,b2,b3,b4;
//声明卡片布局管理器
private CardLayout c1;
public CardLayoutDemo() {
super("卡片布局");
//实例化卡片布局管理器对象
c1 = new CardLayout();
//实例化面板对象
p = new JPanel(c1);
b1 = new JButton("我爱看美剧");
b2 = new JButton("我爱看韩剧");
b3 = new JButton("我爱看日剧");
b4 = new JButton("我爱看电影");
//将组件添加到面板中
p.add("1",b1);
p.add("2",b2);
p.add("3",b3);
p.add("4",b4);
this.add(p);
//c1.last(p);
c1.show(p, "2");
this.setSize(200,150);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
CardLayoutDemo f = new CardLayoutDemo();
f.setVisible(true);
}
}
null布局:
package ch10;
import javax.swing.*;
public class NullDemo extends JFrame{
private JPanel p;
private JButton b1,b2;
public NullDemo() {
super("空布局");
p = new JPanel();
//设置面板布局为空
p.setLayout(null);
b1 = new JButton("确定");
b2 = new JButton("取消");
//设置按钮大小
b1.setBounds(50,60,60,25);
b2.setBounds(160,60,60,25);
//添加
p.add(b1);
p.add(b2);
this.add(p);
this.setSize(300,150);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
NullDemo f = new NullDemo();
f.setVisible(true);
}
}