簡體   English   中英

交換組件時如何鎖定 JSplitPane 分隔符?

[英]How to lock JSplitPane divider when swapping components?

我有一個簡單的擴展 JSplitPane,我在需要時將不同的面板設置為不同的時間。 具體來說,我把它分成上下兩部分,我經常換掉底部。 每次我這樣做時,我都會將 slider position 重置為我想要的位置,但有時它會跳出並重新定位到屏幕頂部(並非總是如此)。

這是我的代碼:

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()”方法來交換底部面板。 有沒有辦法告訴 slider 無論面板的首選尺寸如何,都留在我放置的位置,或者如果沒有,我如何讓面板知道自己適合什么形狀? 感謝您的任何/所有建議!

編輯:我應該注意這些面板不使用布局。 它們是自定義面板,我使用鼠標/鍵盤偵聽器並使用我自己的圖形在它們上面進行繪制。

感謝上面的鏈接,我找到了解決方案。 其實很簡單。 而不是使用

setDividerLocation(height / 4 * 3);

每次添加組件時,我都將其替換為:

setResizeWeight(0.66);

在構造函數中做過一次,它再也沒有打擾我。 0.66 相當於十進制 position 到 h/4*3(我只是試錯了)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM