Tôi đang cố gắng đặt vị trí dải phân cách của JSplitPane nhưng có vẻ như không hoạt động.Đặt vị trí dải phân cách trên JSplitPane không hoạt động
Dưới đây là một SSCCE:
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class JSplitProblem extends JFrame {
public JSplitProblem(){
JPanel upperPanel = new JPanel();
upperPanel.setLayout(new BoxLayout(upperPanel, BoxLayout.X_AXIS));
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
JPanel red = new JPanel();
red.setBackground(Color.red);
leftPanel.add(red);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
JPanel blue = new JPanel();
blue.setBackground(Color.blue);
rightPanel.add(blue);
upperPanel.add(leftPanel);
upperPanel.add(rightPanel);
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(Color.black);
JSplitPane mainSplittedPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperPanel,bottomPanel);
mainSplittedPane.setOneTouchExpandable(true);
mainSplittedPane.setDividerLocation(0.5);
this.add(mainSplittedPane);
this.setSize(800,600);
this.setResizable(true);
this.setVisible(true);
}
public static void main(String[] args) {
new JSplitProblem();
}
}
Tôi muốn bảng điều khiển phía dưới màu đen đặt trên 50% của toàn bộ khu vực theo mặc định. Tôi đang làm gì sai?
đơn giản cộng một cho sscce – mKorbel
IIRC setDividerLocation được thực hiện trong hệ thống phân cấp ngăn chặn. Đó có thể là do phương pháp tính ngay lập tức vị trí thực tế bằng pixel, dựa trên kích thước hiện tại. – jfpoilpret
@jfpoilpret: vâng. Tôi hiểu điều đó từ câu trả lời của mKorbel. Nếu không bằng cách sử dụng gọi sau này sẽ không ảnh hưởng đến nó. Cảm ơn bạn đã chỉ ra. – Heisenbug