我有一個JSplitPane是OneTouchExpandable的JFrame。 我想記住JFrame上JSplitPane的最後一個分隔符位置,如果重新打開JFrame,則處理並恢復位置。如何設置JSplitPane-Divider摺疊/展開狀態?
它工作的很好,但是如果用戶通過oneTouchExpandable UI-Widget擴展一邊,那麼我只在'dispose'中存儲'int'-position,並將'int'-position再次設置爲JFrame-調整JSplitPane-Divider的大小以跳轉到摺疊的組件preferredSize。
如何獲取/設置摺疊/展開狀態?
編輯
現在:調整大小 - 行爲是確定的,但它不是完全一樣的第一次打開相同的行爲 - 因爲現在我沒有MinimumDividerLocation。我想要管理單元,但進一步崩潰狀態。
public class SplitPaneState {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SplitPaneState().createAndSowGUI();
}
});
}
private int position = -1;
private Dimension size = new Dimension(500, 300);
private void createAndSowGUI() {
final JFrame frame = new JFrame("frame");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.getContentPane().add(new JButton(new AbstractAction(){
{
putValue(Action.NAME, "Open Dialog");
}
@Override
public void actionPerformed(ActionEvent e) {
final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JLabel("left Component"), new JLabel("right Component"));
splitPane.setContinuousLayout(true);
splitPane.setOneTouchExpandable(true);
if(position != -1) {
boolean LeftIsCollapsed = position < splitPane.getMinimumDividerLocation();
if(LeftIsCollapsed) {
splitPane.getLeftComponent().setMinimumSize(new Dimension()); // fix by Martijn Courteaux
splitPane.setDividerLocation(0.0d); // fix by Martijn Courteaux
}else {
splitPane.setDividerLocation(position);
}
}
JDialog dialog = new JDialog(frame,"dialog"){
@Override
public void dispose() {
position = splitPane.getDividerLocation();
size = this.getSize();
super.dispose();
}
};
dialog.setSize(size);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(frame);
dialog.getContentPane().add(splitPane);
dialog.setVisible(true);
}
}
));
frame.setVisible(true);
}
}
呀,調整大小,行爲是確定的,但它不是完全一樣的相同的行爲第一次開放的原因現在我沒有MinimumDividerLocation。我想要管理單元,但進一步崩潰狀態。 – oliholz 2011-02-28 15:43:18
嘗試各種建議的解決方案後,我發現以下解決方案可以工作:''pane.getRightComponent()。setMinimumSize(new Dimension()); ';' – 2014-06-20 09:48:45