1
我有一個簡單的擴展JSplitPane,我需要在不同的時間設置不同的面板。具體來說,我把它分成上下兩部分,我經常換掉底部。每次我都會將滑塊位置重置爲我想要的位置,但有時它會跳出並重新定位到屏幕頂部(並非總是)。如何在交換組件時鎖定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 ...控件()」 方法來交換底板。有沒有辦法讓滑塊保持放置在我放置的位置,而不管面板的首選尺寸如何,或者如果不是,我如何讓面板知道如何塑造自己以適應?感謝任何/所有的建議!
編輯:我應該注意到這些面板不使用佈局。它們是我使用鼠標/鍵盤監聽器的自定義面板,並使用我自己的圖形來繪製它們。
請閱讀本文http://stackoverflow.com/questions/6946880/need-the-height-of-an-invalidated-swing-component – mKorbel
「JPanel」默認爲「FlowLayout」,直到您更改爲止;一個[sscce](http://sscce.org/)將有助於澄清問題。 – trashgod