【轉】java中Swing五種常見的佈局方式

一、 邊界佈局(BorderLayout)java

二、流式佈局(FlowLayout)佈局

三、網格佈局(GridLayout)this

四、盒子佈局(BoxLaYout)code

五、空佈局(null)three

還有其餘兩種佈局,分別是GridBagLayout(網格包佈局)、CardLayout(卡片佈局)get

注意:JFrame和JDialog默認佈局爲BorderLayout,JPanel和Applet默認佈局爲FlowLayoutit

邊界佈局示例代碼:io

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();
  }
}

運行結果:class

流式佈局示例代碼:import

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 BoxLayout(this.getContentPane(),BoxLayout.X_AXIS));
		//可使用Box容器代替
		//Box box = new Box(BoxLayout.Y_AXIS);box.add(btn...);box.add(creat..);
		this.add(btn1);
		this.add(btn2);
		this.getContentPane().add(Box.createHorizontalStrut(10)); //採用x佈局時,添加固定寬度組件隔開
		//this.getContentPane().add(Box.createVerticalStrut(5)); //採用y佈局時,添加固定高度組件隔開
		this.add(btn3);
		this.add(btn4);
		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();
	}
}

運行結果:

相關文章
相關標籤/搜索