java中Swing五种常见的布局⽅式
1、边界布局(BorderLayout)
2、流式布局(FlowLayout)
3、⽹格布局(GridLayout)
4、盒⼦布局(BoxLaYout)
5、空布局(null)
还有其他两种布局,分别是GridBagLayout(⽹格包布局)、CardLayout(卡⽚布局)
注意:JFrame和JDialog默认布局为BorderLayout,JPanel和Applet默认布局为FlowLayout
边界布局⽰例代码:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class BorderLayoutExample extends JFrame{
JButton btn1=new JButton("东");
JButton btn2=new JButton("南");
JButton btn3=new JButton("西");
JButton btn4=new JButton("北");
JButton btn5=new JButton("中");
BorderLayoutExample(){
init();
this.setTitle("边界布局");
this.setResizable(true);
this.setSize(200, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
void init(){
this.setLayout(new BorderLayout(10,5)); //默认为0,0;⽔平间距10,垂直间距5
this.add(btn1,BorderLayout.EAST);
this.add(btn2,BorderLayout.SOUTH);
this.add(btn3,BorderLayout.WEST);
this.add(btn4,BorderLayout.NORTH);
this.add(btn5,BorderLayout.CENTER);
}
public static void main(String args[]){
new BorderLayoutExample();
}
}
运⾏结果:
流式布局⽰例代码:
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FlowLayoutExample extends JFrame{
JButton btn1=new JButton("one");
JButton btn2=new JButton("two");
JButton btn3=new JButton("three");
JButton btn4=new JButton("four");
JButton btn5=new JButton("five");
FlowLayoutExample(){
init();
this.setTitle("流式布局");
this.setResizable(true);
this.setSize(200, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
void init(){
this.setLayout(new FlowLayout(FlowLayout.LEFT,10,5)); //默认为居中;⽔平间距10,垂直间距5 this.add(btn1);
this.add(btn2);
this.add(btn3);
this.add(btn4);
this.add(btn5);
}
public static void main(String args[]){
new FlowLayoutExample();
}
}
运⾏结果:
⽹格布局⽰例代码:
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridLayoutExample extends JFrame{
JButton btn1=new JButton("one");
JButton btn2=new JButton("two");
JButton btn3=new JButton("three");
JButton btn4=new JButton("four");
JButton btn5=new JButton("five");
GridLayoutExample(){
init();
this.setTitle("表格布局");
this.setResizable(true);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
void init(){
this.setLayout(new GridLayout(2,3,10,5)); //默认为1⾏,n列;2⾏3列,⽔平间距10,垂直间距5 this.add(btn1);
this.add(btn2);
this.add(btn3);
this.add(btn4);
this.add(btn5);
}
public static void main(String args[]){
new GridLayoutExample();
}
}
运⾏结果:
盒⼦布局⽰例代码:
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class BoxLaYoutExample extends JFrame{
JButton btn1=new JButton("one");
JButton btn2=new JButton("two");
JButton btn3=new JButton("three");
JButton btn4=new JButton("four");
JButton btn5=new JButton("five");
BoxLaYoutExample(){
init();
this.setTitle("表格布局");
this.setResizable(true);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
void init(){
this.setLayout(new ContentPane(),BoxLayout.X_AXIS));
/
/可以使⽤Box容器代替
//Box box = new Box(BoxLayout.Y_AXIS);box.);box.add(creat..);
this.add(btn1);
this.add(btn2);
this.add(btn4);javaswing实现购买
this.add(btn5);
}
public static void main(String args[]){
new BoxLaYoutExample();
}
}
运⾏结果:
空布局⽰例代码:
import javax.swing.JButton;
import javax.swing.JFrame;
public class NullLayoutExample extends JFrame{
JButton btn1=new JButton("one");
JButton btn2=new JButton("two");
JButton btn3=new JButton("three");
JButton btn4=new JButton("four");
JButton btn5=new JButton("five");
NullLayoutExample(){
init();
this.setTitle("空布局");
this.setResizable(true);
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
void init(){
this.setLayout(null);
btn1.setBounds(10, 0, 100, 50); //x坐标10,y坐标0,组件宽100,⾼50 btn2.setBounds(20, 50, 100, 50);
btn3.setBounds(30, 100, 100, 50);
btn4.setBounds(40, 150, 100, 50);
btn5.setBounds(50, 200, 100, 50);
this.add(btn1);
this.add(btn2);
this.add(btn3);
this.add(btn4);
this.add(btn5);
}
public static void main(String args[]){
new NullLayoutExample();
}
}
运⾏结果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论