1

我有一个简单的扩展 JSplitPane,我在需要时将不同的面板设置为不同的时间。具体来说,我把它分成上下两部分,我经常换掉底部。每次我这样做时,我都会将滑块位置重置为我想要的位置,但有时它会跳出并重新定位到屏幕顶部(并非总是如此)。

这是我的代码:

public class MainPanel extends JSplitPane{

    public Screen screen;

    public int height;

    public ControlPanel curPanel;

    public MainPanel(Screen screen, int height){
        super(JSplitPane.VERTICAL_SPLIT);

        this.screen = screen;
        this.height = height;

        setDividerSize(2);
        setEnabled(false);

        setTopComponent(screen);

        setToInitControls();
    }

    public void setToInitControls(){
        InitControls initCtrls = new InitControls(this);
        setBottomComponent(initCtrls);
        curPanel = initCtrls;
        setDividerLocation(height / 4 * 3);
    }

    public void setToConfigControls(){
        ConfigControls configCtrls = new ConfigControls(this);
        setBottomComponent(configCtrls);
        curPanel = configCtrls;
        setDividerLocation(height / 4 * 3);
    }

    public void setToWaitControls(){
        WaitControls waitCtrls = new WaitControls(this);
        setBottomComponent(null);
        setBottomComponent(waitCtrls);
        curPanel = waitCtrls;
        setDividerLocation(height / 4 * 3);
    }

    //and so on (I have more methods like these further down)

    //OVERRIDES: I figured overriding these might help. It didn't.
    @Override
    public int getMinimumDividerLocation(){
        return (height / 4 * 3);
    }
    @Override
    public int getMaximumDividerLocation(){
        return (height / 4 * 3);
    }
}

基本上,我使用“setTo...Controls()”方法来交换底部面板。有没有办法告诉滑块保持在我放置的位置,而不管面板的首选尺寸如何,或者如果没有,我如何让面板知道自己要适应什么形状?感谢您的任何/所有建议!

编辑:我应该注意这些面板不使用布局。它们是自定义面板,我使用鼠标/键盘侦听器并使用我自己的图形在它们上面进行绘制。

4

1 回答 1

0

感谢上面的链接,我找到了解决方案。其实很简单。而不是使用

setDividerLocation(height / 4 * 3);

每次添加组件时,我都将其替换为:

setResizeWeight(0.66);

在构造函数中做过一次,它再也没有打扰我。0.66 是 h/4*3 的等效小数位(我只是试错了)。

于 2011-08-15T19:34:22.357 回答