Swing五種常見的布局器
布局器是用在容器上的檀轨。 用來決定容器上的組件擺放的位置和大小
示例 1 : 絕對定位
絕對定位就是指不使用布局器养匈,組件的位置和大小需要單獨指定
package gui;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
// 設(shè)置布局器為null乡括,即進(jìn)行絕對定位基公,容器上的組件都需要指定位置和大小
f.setLayout(null);
JButton b1 = new JButton("英雄1");
// 指定位置和大小
b1.setBounds(50, 50, 80, 30);
JButton b2 = new JButton("英雄2");
b2.setBounds(150, 50, 80, 30);
JButton b3 = new JButton("英雄3");
b3.setBounds(250, 50, 80, 30);
// 沒有指定位置和大小辉词,不會出現(xiàn)在容器上
JButton b4 = new JButton("英雄3");
f.add(b1);
f.add(b2);
f.add(b3);
// b4沒有指定位置和大小就缆,不會出現(xiàn)在容器上
f.add(b4);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
示例 2 : FlowLayout
設(shè)置布局器為FlowLayout,順序布局器
容器上的組件水平擺放
加入到容器即可斯碌,無需單獨指定大小和位置
FlowLayout
package gui;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
// 設(shè)置布局器為FlowLayerout
// 容器上的組件水平擺放
f.setLayout(new FlowLayout());
JButton b1 = new JButton("英雄1");
JButton b2 = new JButton("英雄2");
JButton b3 = new JButton("英雄3");
// 加入到容器即可晒来,無需單獨指定大小和位置
f.add(b1);
f.add(b2);
f.add(b3);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
示例 3 : BorderLayout
設(shè)置布局器為BorderLayout
容器上的組件按照上北 下南 左西 右東 中的順序擺放
BorderLayout
package gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
// 設(shè)置布局器為BorderLayerout
// 容器上的組件按照上北下南左西右東中的順序擺放
f.setLayout(new BorderLayout());
JButton b1 = new JButton("洪七");
JButton b2 = new JButton("段智興");
JButton b3 = new JButton("歐陽鋒");
JButton b4 = new JButton("黃藥師");
JButton b5 = new JButton("周伯通");
// 加入到容器的時候,需要指定位置
f.add(b1, BorderLayout.NORTH);
f.add(b2, BorderLayout.SOUTH);
f.add(b3, BorderLayout.WEST);
f.add(b4, BorderLayout.EAST);
f.add(b5, BorderLayout.CENTER);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
示例 4 : GridLayout
GridLayout周伦,即網(wǎng)格布局器
GridLayout
package gui;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
// 設(shè)置布局器為GridLayerout夕春,即網(wǎng)格布局器
// 該GridLayerout的構(gòu)造方法表示該網(wǎng)格是2行3列
f.setLayout(new GridLayout(2, 3));
JButton b1 = new JButton("洪七");
JButton b2 = new JButton("段智興");
JButton b3 = new JButton("歐陽鋒");
JButton b4 = new JButton("黃藥師");
JButton b5 = new JButton("周伯通");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
示例 5 : setPreferredSize
即便 使用 布局器 ,也可以 通過setPreferredSize专挪,向布局器建議該組件顯示的大小.
注 :只對部分布局器起作用及志,比如FlowLayout可以起作用。 比如GridLayout就不起作用狈蚤,因為網(wǎng)格布局器必須對齊
setPreferredSize
package gui;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestGUI {
public static void main(String[] args) {
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(new FlowLayout());
JButton b1 = new JButton("英雄1");
JButton b2 = new JButton("英雄2");
JButton b3 = new JButton("英雄3");
// 即便 使用 布局器 困肩,也可以 通過setPreferredSize划纽,向布局器建議該組件顯示的大小
b3.setPreferredSize(new Dimension(180, 40));
f.add(b1);
f.add(b2);
f.add(b3);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
示例 6 : CardLayout
CardLayout需要用到面板和JComboBox
CardLayout
更多內(nèi)容脆侮,點擊了解: Swing五種常見的布局器