Computer >> Máy Tính >  >> Lập trình >> Java

Làm cách nào chúng ta có thể đặt màu nền thành JSplitPane trong Java?


A JSplitPane là một lớp con của JComponent lớp cho phép chúng tôi sắp xếp hai thành phần cạnh nhau theo chiều ngang hoặc theo chiều dọc trong một ngăn duy nhất. Người dùng cũng có thể điều chỉnh vùng hiển thị của cả hai thành phần trong thời gian chạy. Các phương thức quan trọng của JSplitPane là remove (), removeAll (), resetToPreferredSizes () và setDividerLocation () . JSplitPane có thể tạo PropertyChangeListener giao diện. Chúng tôi có thể đặt màu nền vào JSplitPane bằng cách thêm hai màu nền khác nhau vào hai bảng trước và chuyển các đối số này tới JSplitPane phương thức khởi tạo.

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();
   }
}

Đầu ra

Làm cách nào chúng ta có thể đặt màu nền thành JSplitPane trong Java?