花了一段时间(可能是因为这里很早:-)来理解问题,所以只是确保我明白了:
- 底部组件的大小可以是用户始终决定的大小
- 调整框架大小时,顶部组件应发生所有高度变化
- 有一个还原为默认大小的选项,与之前的任何设置无关
- “默认”表示底部组件的固定高度必须为xx
如果这样,解决方案是将框架调整大小与底部组件的大小分开。第二个选择是无效的:调整框架大小并将底部comp调整大小包装到invokeLater中(EventQueue或SwingUtilities,无关紧要)。
void restoreDefaults() { f.setSize(f.getWidth(), getDesktopRect(f.getGraphicsConfiguration()).height); SwingUtilities.invokeLater(new Runnable() { public void run() { sp.setDividerLocation(sp.getSize().height - 100); } });}这样可以保证按预期工作,因为invokeLater在所有已排队的事件之后将请求放到最后:
/** * Causes <i>doRun.run()</i> to be executed asynchronously on the * AWT event dispatching thread. This will happen after all * pending AWT events have been processed. [...] * If invokeLater is called from the event dispatching thread -- * for example, from a JButton's ActionListener -- the <i>doRun.run()</i> will * still be deferred until all pending events have been processed.



