Java 交换组件时如何锁定JSplitPane分配器?
我有一个简单的扩展JSplitPane,我在需要的不同时间设置不同的面板。具体地说,我将它分为上下两部分,并经常交换底部部分。每次我这样做时,我都会将滑块的位置重置为我想要的位置,但有时它会跳转到屏幕顶部,并将自身重新定位到屏幕顶部(并非总是如此) 这是我的密码:Java 交换组件时如何锁定JSplitPane分配器?,java,swing,jpanel,jsplitpane,Java,Swing,Jpanel,Jsplitpane,我有一个简单的扩展JSplitPane,我在需要的不同时间设置不同的面板。具体地说,我将它分为上下两部分,并经常交换底部部分。每次我这样做时,我都会将滑块的位置重置为我想要的位置,但有时它会跳转到屏幕顶部,并将自身重新定位到屏幕顶部(并非总是如此) 这是我的密码: public class MainPanel extends JSplitPane{ public Screen screen; public int height; public ControlPane
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()”方法交换底部面板。有没有一种方法可以告诉滑块保持在我放置它的位置,而不管面板的首选尺寸,或者如果没有,我如何使面板知道自己的形状以适应?谢谢你的建议
编辑:我应该注意这些面板不使用布局。它们是我使用鼠标/键盘监听器的自定义面板,并使用我自己的图形在其上绘制。多亏了上面的链接,我找到了解决方案。其实很简单。而不是使用
setDividerLocation(height / 4 * 3);
每次我添加一个组件时,我都会将其替换为:
setResizeWeight(0.66);
在构造器中做过一次,我再也没有感到困扰。0.66相当于h/4*3的小数点位置(我刚刚试过并出错)。请阅读此
JPanel
默认为FlowLayout
,直到您更改它;调查将有助于澄清问题。