JSplitPane là một lớp con của JComponent cho phép sắp xếp hai thành phần cạnh nhau ngang (horizontally) hoặc dọc (vertically) trong một pane. Khu vực hiển thị của cả hai thành phần cũng có thể điều chỉnh lúc runtime bởi người dùng. Các phương thức quan trọng của JSplitPane bao gồm remove(), removeAll(), resetToPreferredSizes() và setDividerLocation(). JSplitPane tạo ra giao diện PropertyChangeListener. Chúng ta có thể đặt màu nền cho JSplitPane bằng cách thêm hai màu nền khác nhau cho hai panel trước rồi truyền vào constructor JSplitPane.
Ví dụ
import javax.swing.*;
import java.awt.*;
public class JSplitPaneColorTest extends JFrame {
private JSplitPane jsp;
private JPanel panel1, panel2;
public JSplitPaneColorTest() {
setTitle("JSplitPane Example");
panel1 = new JPanel();
panel1.setBackground(Color.lightGray);
panel2 = new JPanel();
panel2.setBackground(Color.blue);
jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panel1, panel2);
jsp.setDividerSize(10);
jsp.setResizeWeight(0.5);
add(jsp);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 275);
setVisible(true);
}
public static void main(String args[]) {
new JSplitPaneColorTest();
}
}
Kết quả
