java JSplitPane 精确分割 50%

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2311449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:33:43  来源:igfitidea点击:

JSplitPane splitting 50% precisely

javaswingjcomponent

提问by user278731

In Swing, what's the best way to make the JSplitPane to split two jpanels with 50% size each.

在 Swing 中,让 JSplitPane 拆分两个 jpanel 的最佳方法是什么,每个 jpanel 的大小为 50%。

It looks like if I don't set preferred sizes on the panels it always makes the first panel almost invisible (2%) and the second one (98%)

看起来如果我不在面板上设置首选尺寸,它总是会使第一个面板几乎不可见 (2%) 和第二个 (98%)

Thanks in advance

提前致谢

回答by Adamski

You should use setDividerLocation(double proportionalLocation)to determine the initial space distributionof the JSplitPane, and then call setResizeWeight(double)with the same value to ensure that the panes are resized in proportion.

您应该使用setDividerLocation(double proportionalLocation)来确定初始空间分配JSplitPane,然后调用setResizeWeight(double)具有相同值,以确保窗格中的比例调整。

Also, be aware: Calling setDividerLocation(double)before the JSplitPaneis visible will not work correctly, as the space calculation is based on the Component's current size. Instead you need to involve a nasty hack, such as overriding the JPanel's paint method that contains the JSplitPane:

另外,请注意:setDividerLocation(double)JSplitPane可见之前调用将无法正常工作,因为空间计算基于Component的当前大小。相反,您需要涉及一个讨厌的黑客,例如覆盖JPanel包含 的 的paint 方法JSplitPane

private boolean painted;

@Override
public void paint(Graphics g) {
    super.paint(g);

    if (!painted) {
        painted = true;
        splitPane.setDividerLocation(0.25);
    }
}

回答by Peter Lang

Use

利用

setResizeWeight(.5d);

setResizeWeight(.5d);

[...] A value of 0, the default, indicates the right/bottom component gets all the extra space (the left/top component acts fixed), where as a value of 1 specifies the left/top component gets all the extra space (the right/bottom component acts fixed). [...]

[...] 默认值为 0 表示右/底部组件获得所有额外空间(左/顶部组件固定),其中值为 1 指定左/顶部组件获得所有额外空间空间(右侧/底部组件是固定的)。[...]

回答by William Dutton

I had a similar problem and I solved it by using a component listener in the parent container and set the divider location on the first resize. Initialise a variable firstResize to true and add this into the parent container constructor:

我遇到了类似的问题,我通过在父容器中使用组件侦听器并在第一次调整大小时设置分隔符位置来解决它。将变量 firstResize 初始化为 true 并将其添加到父容器构造函数中:

addComponentListener(new ComponentAdapter(){
    @Override
    public void componentResized(ComponentEvent e) {
        if(firstResize){
            splitPane.setDividerLocation(0.5);
            firstResize = false;
        }
    }
});

This should cause the divider to be centred when the parent container size is first set.

当第一次设置父容器大小时,这应该会导致分隔线居中。

回答by John

The solutions here do not take into account the case where the user moves the divider (i.e. a variable divider location). A complete example that takes this into account is available here:

这里的解决方案没有考虑用户移动分隔符(即可变分隔符位置)的情况。考虑到这一点的完整示例可在此处获得:

How do you get JSplitPane to keep the same proportional location if the user has moved the location of the divider

如果用户移动了分隔线的位置,如何让 JSplitPane 保持相同的比例位置