package swing;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test3 {
public static void main(String[] args) {
new Box1().setVisible(true);
}
}
class Border extends JFrame{
public Border(){
super("边界布局");
this.setSize(300,300);
this.setLocation(500,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建窗体可自由变化大小
this.setResizable(true);
//设置布局格式位边界布局,水品间距10,垂直间距5
this.setLayout(new BorderLayout(10,5));
JButton jb1 =new JButton("东");
JButton jb2 =new JButton("西");
JButton jb3 =new JButton("南");
JButton jb4 =new JButton("北");
JButton jb5 =new JButton("中");
this.add(jb1,BorderLayout.EAST);
this.add(jb2,BorderLayout.SOUTH);
this.add(jb3,BorderLayout.WEST);
this.add(jb4,BorderLayout.NORTH);
this.add(jb5,BorderLayout.CENTER);
}
}
class Flow extends JFrame{
public Flow(){
super("流式布局");
this.setSize(200,200);
this.setLocation(500,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建窗体可自由变化大小
this.setResizable(true);
//设置流式布局,默认为居中,设置靠左,水平间距10,垂直间距5
this.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
JButton jb1 =new JButton("one");
JButton jb2 =new JButton("two");
JButton jb3 =new JButton("three");
JButton jb4 =new JButton("four");
JButton jb5 =new JButton("five");
this.add(jb1);
this.add(jb2);
this.add(jb3);
this.add(jb4);
this.add(jb5);
}
}
class Table extends JFrame{
public Table(){
super("表格布局");
this.setSize(500,200);
this.setLocation(500,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建窗体可自由变化大小
this.setResizable(true);
//设置表格布局,默认1行n列,设置2行3列,水平间距10,垂直间距5
this.setLayout(new GridLayout(2,3,10,5));
JButton jb1 =new JButton("one");
JButton jb2 =new JButton("two");
JButton jb3 =new JButton("three");
JButton jb4 =new JButton("four");
JButton jb5 =new JButton("five");
this.add(jb1);
this.add(jb2);
this.add(jb3);
this.add(jb4);
this.add(jb5);
}
}
class Box1 extends JFrame{
public Box1(){
super("盒子布局");
this.setSize(500,200);
this.setLocation(500,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建窗体可自由变化大小
this.setResizable(true);
//设置盒子布局,
this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.LINE_AXIS));
JButton jb1 =new JButton("one");
JButton jb2 =new JButton("two");
JButton jb3 =new JButton("three");
JButton jb4 =new JButton("four");
JButton jb5 =new JButton("five");
this.add(jb1);
this.add(jb2);
//采用X布局时,添加宽度组件隔开
this.getContentPane().add(Box.createHorizontalStrut(10));
this.add(jb3);
this.add(jb4);
//采用Y布局时,添加高度组件隔开
this.getContentPane().add(Box.createVerticalStrut(20));
this.add(jb5);
}
}
swing学习5--界面常见布局方式
猜你喜欢
转载自www.cnblogs.com/xn136234/p/9713081.html
今日推荐
周排行