java swing 常用的三種佈局方式:邊界佈局、流佈局、網格佈局管理器
阿新 • • 發佈:2018-12-10
作者:firstmiki
來源:firstmiki的部落格 著作權歸作者所有,轉載請聯絡作者獲得授權。
這篇博文僅僅簡單介紹了三種常見的佈局管理器,都是一些簡單應用;
一、 邊界佈局管理器(FlowLayout)
/*
* 功能:演示邊界佈局管理器:元件的位置和大小
*/
package GUI;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
/*Date: 2017年1月21日 Time: 下午4:59:40
@firstmiki ---blog.ppt1234.com*/
public class TestBorderLayout extends JFrame{ //0.繼承JFrame
//1. 定義元件
JButton jButton, jButton2,jButton3,jButton4,jButton5;
public TestBorderLayout() {
//2. 建立元件
jButton = new JButton("中間");
jButton2 = new JButton("北邊");
jButton3 = new JButton("西邊");
jButton4 = new JButton("東邊");
jButton5 = new JButton("南邊");
//3. 新增各個元件
this.add(jButton, BorderLayout.CENTER); //佈局的中間
// this.add(jButton2, BorderLayout.NORTH); //佈局的北邊
// this.add(jButton3, BorderLayout.WEST); //佈局的西邊
this.add(jButton4, BorderLayout.EAST); //佈局的東邊
this.add(jButton5, BorderLayout.SOUTH); //佈局的南邊
//4. 設定窗體屬性
this.setTitle("演示邊界佈局管理器");
this.setSize(300, 200);
this.setLocation(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
TestBorderLayout testBorderLayout = new TestBorderLayout();
}
}
二、 流佈局管理器(FlowLayout)
/*
* 功能:演示流佈局管理器:元件的位置和大小
*/
package GUI;
import java.awt.*;
import javax.swing.*;
/*Date: 2017年1月21日 Time: 下午4:59:40
@firstmiki ---blog.ppt1234.com*/
//邊界佈局管理器
public class TestFlowLayout extends JFrame{ //0.繼承JFrame
//1. 定義元件
JButton jButton1, jButton2,jButton3,jButton4,jButton5;
public TestFlowLayout() {
//2. 建立元件
jButton1 = new JButton("A");
jButton2 = new JButton("B");
jButton3 = new JButton("C");
jButton4 = new JButton("D");
jButton5 = new JButton("E");
//3. 新增各個元件
this.add(jButton1);
this.add(jButton2);
this.add(jButton3);
this.add(jButton4);
this.add(jButton5);
//設定流佈局
// this.setLayout(new FlowLayout()); //預設佈局方式為居中
this.setLayout(new FlowLayout(FlowLayout.LEFT));
//4. 設定窗體屬性
this.setTitle("演示流佈局管理器"); //設定標題
this.setSize(200, 200); //設定
this.setLocation(200, 200); //設定窗體出現的位置
this.setVisible(true); //設定窗體可見
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設定窗體關閉的同時關閉jvm
this.setResizable(false); //Resizable:可調整大小的,設定窗體大小不可變
}
public static void main(String[] args) {
TestFlowLayout testBorderLayout = new TestFlowLayout();
}
}
三、 網格佈局管理器(GridLayout)
/**
* 功能:演示網格佈局管理器
*/
package GUI;
/*Date: 2017年1月22日 Time: 下午12:58:40
@firstmiki ---blog.ppt1234.com*/
import java.awt.*;
import javax.swing.*;
public class TestGridLayout extends JFrame{
//定義元件
int size = 9;
//定義按鈕陣列
JButton jButton[] = new JButton[size];
//建構函式
public TestGridLayout() {
//建立元件
for(int i = 0; i<size; i++){
jButton[i] = new JButton(String.valueOf(i+1));
}
//新增元件
for(int i = 0; i<size; i++){
this.add(jButton[i]);
}
//設定網格佈局
this.setLayout(new GridLayout(3, 3, 10, 30));
//設定窗格屬性
this.setTitle("演示網格佈局管理器");
this.setSize(400, 400);
this.setLocation(200, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false); //Resizable:可調整大小的
}
public static void main(String[] args) {
TestGridLayout testGridLayout = new TestGridLayout();
}
}