java界面,BorderLayout,GridLayout,FlowLayout

BorderLayout類

BorderLayout將版面分成EAST(東),SOUTH(南),WEST(西),NORTH(北),CENTER(中),五個區域塊,並語序將組件放在指定的區域塊內,因爲將組件放入BorderLayout版面內會造成組件變形,所以不適合加入JButton(按鈕)這類組件,而比較合適JPanel(面板);


package hhxy.Test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BorderLayoutEx {
 JFrame frame;
 public BorderLayoutEx(){
	 frame=new JFrame("xx書店信息系統");
	 Image image=Toolkit.getDefaultToolkit().createImage("a1.jpg");
	 frame.setIconImage(image);//把圖片添加到標題欄
	 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關閉
	 frame.setContentPane(createContentPane());//創建ContentPane
	 frame.setLocation(400,250);//設置窗口彈出的位置
	 frame.setSize(300, 250);//設置窗口的大小
	 frame.setVisible(true);//設置組件可見
 }

	private Container createContentPane() {
	Container contentPane=new Container();
	//將窗口版面設置爲BorderLayout,並設置內部組件間的水平距離爲5像素,垂直爲10像素
	contentPane.setLayout(new BorderLayout(5, 10));
	JPanel[] panel=new JPanel[5];//創建面板對象
	for(int i=0; i<panel.length;i++){
		panel[i]=new JPanel();
		panel[i].setBackground(Color.red);
	}
	contentPane.add(panel[0],BorderLayout.EAST);//將組件放在指定區域
	contentPane.add(panel[1],BorderLayout.SOUTH);//將組件放在指定區域
	contentPane.add(panel[2],BorderLayout.WEST);//將組件放在指定區域
	contentPane.add(panel[3],BorderLayout.NORTH);//將組件放在指定區域
	contentPane.add(panel[4],BorderLayout.CENTER);//將組件放在指定區域
	return contentPane;
}
	public static void main(String[] args) {
		new BorderLayoutEx();
	}
}
輸出結果:


GridLayout類

GridLayout所切割出來的版面就如同表格版整齊,加入的組件會按照順序從左到右從上到下襬放,所以無法直接指定要擺放的區域,組件放入後會變成方形,所以不適合加入JButton這類組件,適合加入JPanel。

代碼如下:

package hhxy.Test;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GridLayoutEx {
    JFrame frame;
    public GridLayoutEx(){
    	frame=new JFrame("XX圖書信息系統");
    	frame.setContentPane(createContentPane());
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLocation(350, 200);
    	frame.setSize(400,250);
    	frame.setVisible(true);
    }
	private Container createContentPane() {
		Container contentPane=new Container();
		//將窗口版面設置成2X3的表格型;並且設定組件間的水平距離爲5像素,垂直爲10像素
		contentPane.setLayout(new GridLayout(2,3,5,10));
		JPanel[] panel=new JPanel[5];
		for(int i=0;i<panel.length;i++){
			panel[i]=new JPanel();
			panel[i].setBackground(Color.green);
			contentPane.add(panel[i]);
		}
		return contentPane;
	}
	public static void main(String[] args) {
          new GridLayoutEx();
	}

}
結果如圖:


FlowLayout類

FlowLayout的配置方式也是從左到右,從上到下,排列,如果窗口寬度足夠,會將所有的語速放在同一行;如果苦讀不夠,自動換行。FlowLayout默認組件居中對齊,也可以按照需求左對齊或者右對齊 ,加入FlowLayout版面的組件也是按照順序擺放的,所以也無法直接指定要擺放的位置。組價不會變形可以加入JButton這類組件。
代碼:
package hhxy.Test;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class FlowLayoutEx {
    JFrame frame;
    
	public FlowLayoutEx(){
	  frame =new JFrame("xx圖書信息系統");
	  frame.setContentPane(createPane());
	  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  frame.setSize(500,250);
	  frame.setLocation(400, 250);
	  frame.setVisible(true);
	}
	private Container createPane() {
		Container contentPane=new Container();
		contentPane.setLayout(new FlowLayout(FlowLayout.LEFT,5,10));
		JButton[] button=new JButton[5];
		for(int i=0;i<button.length;i++){
			button[i]=new JButton("button"+i);
			contentPane.add(button[i]);
		}
		return contentPane;
	}
	public static void main(String[] args) {
		new FlowLayoutEx();
	}

}
結果:



發佈了92 篇原創文章 · 獲贊 38 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章