Layout managers cho phép kiểm soát cách sắp xếp các component trực quan trong form GUI bằng cách xác định kích thước và vị trí component trong container.
Các loại LayoutManager
Có 6 layout manager trong Java:
- FlowLayout: Sắp xếp component như từ trên trang, điền từ trái sang phải, trên xuống dưới. Component theo thứ tự thêm. Nếu container không đủ rộng, tự xuống dòng. Khoảng cách dọc/ngang có thể điều khiển. Component có thể căn trái, giữa, phải.
- BorderLayout: Sắp xếp component theo các cạnh hoặc giữa container: trên, dưới, phải, trái. Component thêm vào trên/dưới lấy chiều cao ưu tiên, chiều rộng bằng container. Component thêm vào trái/phải lấy chiều rộng ưu tiên, chiều cao bằng phần còn lại. Component thêm vào giữa chiếm phần diện tích còn lại.
- GridLayout: Sắp xếp component thành lưới ô vuông bằng nhau, thêm từ trái sang phải, trên xuống dưới. Mỗi ô chỉ chứa 1 component, mọi vùng cùng kích thước. Resize container thì tất cả ô tự resize. Thứ tự đặt component theo thứ tự thêm.
- GridBagLayout: Layout mạnh mẽ sắp xếp component thành lưới ô và giữ tỷ lệ khung hình khi resize container. Ô có thể khác kích thước. Gán khoảng cách ngang/dọc nhất quán. Cho phép chỉ định căn chỉnh mặc định cho component trong cột/hàng.
- BoxLayout: Sắp xếp nhiều component theo dọc hoặc ngang, không phải cả hai. Component xếp từ trái sang phải hoặc trên xuống dưới. Nếu căn ngang, chiều cao tất cả component bằng nhau = component lớn nhất. Nếu căn dọc, chiều rộng tất cả component bằng nhau = component rộng nhất.
- CardLayout: Sắp xếp 2 hoặc nhiều component cùng kích thước. Component xếp chồng như bộ bài, chỉ bài trên cùng hiển thị. Component thêm đầu tiên ở trên cùng. Khoảng cách mặc định 0, hiển thị ngang hoặc dọc.
Ví dụ
import java.awt.*;
import javax.swing.*;
public class LayoutManagerTest extends JFrame {
JPanel flowLayoutPanel1, flowLayoutPanel2, gridLayoutPanel1, gridLayoutPanel2, gridLayoutPanel3;
JButton one, two, three, four, five, six;
JLabel bottom, lbl1, lbl2, lbl3;
public LayoutManagerTest() {
setTitle("LayoutManager Test");
setLayout(new BorderLayout()); // Set BorderLayout for JFrame
flowLayoutPanel1 = new JPanel();
one = new JButton("One");
two = new JButton("Two");
three = new JButton("Three");
flowLayoutPanel1.setLayout(new FlowLayout(FlowLayout.CENTER)); // Set FlowLayout Manager
flowLayoutPanel1.add(one);
flowLayoutPanel1.add(two);
flowLayoutPanel1.add(three);
flowLayoutPanel2 = new JPanel();
bottom = new JLabel("This is South");
flowLayoutPanel2.setLayout(new FlowLayout(FlowLayout.CENTER)); // Set FlowLayout Manager
flowLayoutPanel2.add(bottom);
gridLayoutPanel1 = new JPanel();
gridLayoutPanel2 = new JPanel();
gridLayoutPanel3 = new JPanel();
lbl1 = new JLabel("One");
lbl2 = new JLabel("Two");
lbl3 = new JLabel("Three");
four = new JButton("Four");
five = new JButton("Five");
six = new JButton("Six");
gridLayoutPanel2.setLayout(new GridLayout(1, 3, 5, 5)); // Set GridLayout Manager
gridLayoutPanel2.add(lbl1);
gridLayoutPanel2.add(lbl2);
gridLayoutPanel2.add(lbl3);
gridLayoutPanel3.setLayout(new GridLayout(3, 1, 5, 5)); // Set GridLayout Manager
gridLayoutPanel3.add(four);
gridLayoutPanel3.add(five);
gridLayoutPanel3.add(six);
gridLayoutPanel1.setLayout(new GridLayout(2, 1)); // Set GridLayout Manager
gridLayoutPanel1.add(gridLayoutPanel2);
gridLayoutPanel1.add(gridLayoutPanel3);
add(flowLayoutPanel1, BorderLayout.NORTH);
add(flowLayoutPanel2, BorderLayout.SOUTH);
add(gridLayoutPanel1, BorderLayout.CENTER);
setSize(400, 325);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
new LayoutManagerTest();
}
}
Kết quả
