gpt4 book ai didi

java - 在 JSplitPane 上设置分隔线位置不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 21:19:06 26 4
gpt4 key购买 nike

我正在尝试设置 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%。我做错了什么?

最佳答案

如果您希望拆分 Pane 的两半共享拆分 Pane 的额外或删除空间,请将调整大小权重设置为 0.5:( Tutorial )

JSplitPane mainSplittedPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upperPanel,bottomPanel);
mainSplittedPane.setOneTouchExpandable(true);
mainSplittedPane.setResizeWeight(0.5);

关于java - 在 JSplitPane 上设置分隔线位置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7625762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com