[英]Setting divider location on a JSplitPane doesn't work
我正在嘗試設置JSplitPane的分隔符位置,但似乎無法正常工作。
這是一個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();
}
}
我希望默認情況下黑底板可以占據整個區域的50%。 我究竟做錯了什么?
如果您希望拆分窗格的兩半在拆分窗格的額外或刪除空間中共享,請將調整大小權重設置為0.5 :( 教程 )
JSplitPane mainSplittedPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperPanel,bottomPanel);
mainSplittedPane.setOneTouchExpandable(true);
mainSplittedPane.setResizeWeight(0.5);
1)PrefferedSize必須返回Childs而不是我錯誤設置在我的情況下:-),然后我的答案不是@kleopatra抵抗
2)將有關JSplitPane
rezize,size等所有內容放入invokeLater()
。
。
import java.awt.*;
import javax.swing.*;
public class JSplitProblem extends JFrame {
private static final long serialVersionUID = 1L;
private JSplitPane mainSplittedPane;
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);
mainSplittedPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperPanel, bottomPanel);
mainSplittedPane.setOneTouchExpandable(true);
mainSplittedPane.setDividerLocation(0.5);
add(mainSplittedPane);
setPreferredSize(new Dimension(400, 300));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(true);
setVisible(true);
pack();
restoreDefaults();
}
private void restoreDefaults() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
mainSplittedPane.setDividerLocation(mainSplittedPane.getSize().height /2);
//mainSplittedPane.setDividerLocation(mainSplittedPane.getSize().width /2);
}
});
}
public static void main(String[] args) {
JSplitProblem jSplitProblem = new JSplitProblem();
}
}
我不確定,但我認為你應該嘗試pack()
你的框架。 如果這不起作用,請在打包框架后嘗試重置分隔符位置。
只需添加下面的代碼,這就足夠了。
mainSplittedPane.setResizeWeight(0.5);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.