這篇博文僅僅簡單介紹了三種常見的布局管理器,都是一些簡單應用;
一、 邊界布局管理器(FlowLayout)
package GUI;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
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.*;
//邊界布局管理器
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;
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
jButton[i] = new JButton(String.valueOf(i+1));
}
//添加元件
for(int i = 0; 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();
}
}
效果為:
下面讓我們來看一個小例子吧!我們先來實作一個課程表:
import javax.swing.*;
public class KCB {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
JTable jTable = new JTable(new KCBData());
JScrollPane jScrollPane = new JScrollPane(jTable);
jFrame.add(jScrollPane);
jFrame.pack();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class KCBData implements TableModel {
private String[] title = {"周一", "周二", "周三", "周四", "周五", "周六", "周天"};
private String[][] data = new String[8][7];
public KCBData() {
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
data[i][j] = "";
}
}
}
@Override
public int getRowCount() {
return 8;
}
@Override
public int getColumnCount() {
return 7;
}
@Override
public String getColumnName(int columnIndex) {
return title[columnIndex];
}
@Override
public Class> getColumnClass(int columnIndex) {
return String.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
data[rowIndex][columnIndex] = (String) aValue;
}
@Override
public void addTableModelListener(TableModelListener l) {
}
@Override
public void removeTableModelListener(TableModelListener l) {
}
}