在窗口关闭时保存JXMultiSplitPane的布局定义

时间:2019-04-03 20:27:25

标签: swingx

我正在使用JXMultiSplitPane,并且希望能够在用户关闭窗口时保存关联的布局定义。我的问题是我找不到找到实际布局定义的方法,例如调整布局的权重。

如果您运行下面的代码,则当您打开窗口时,它会打印出模型定义,这与您在layoutDef中的定义匹配,并且在关闭窗口时也是如此。如果在打开窗口时拖动分隔线,我希望在移动权重时可以调整权重,但是打印出的定义没有改变。

有人知道您是否可以获得实际的权重,而不是创建JXMultiSplitPane时的权重,以便您可以保留设置以便下次用户打开同一窗口时使用?

package com.jf.example;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.JXMultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout;


public class App
{
    public static void main( String[] args )
    {
        SwingUtilities.invokeLater(() -> {

            final JXMultiSplitPane msp = new JXMultiSplitPane();
            final String layoutDef = "(COLUMN (ROW weight=0.8 (COLUMN weight=0.25 " +
                      "(LEAF name=left.top weight=0.5) (LEAF name=left.middle weight=0.5))" +
                      "(LEAF name=editor weight=0.75)) (LEAF name=bottom weight=0.2))";

            final MultiSplitLayout multiSplitLayout = new MultiSplitLayout(MultiSplitLayout.parseModel(layoutDef));
            msp.setLayout(multiSplitLayout);                

            JFrame frame = new JFrame("FrameDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    MultiSplitLayout.printModel(multiSplitLayout.getModel());
                }
            });

            msp.add( new JButton( "Left Top" ), "left.top" );
            msp.add( new JButton( "Left Middle" ), "left.middle" );
            msp.add( new JButton( "Editor" ), "editor" );
            msp.add( new JButton( "Bottom" ), "bottom" );

            frame.setPreferredSize(new Dimension(600, 800));
            frame.getContentPane().add(msp, BorderLayout.CENTER );      
            frame.pack();
            frame.setVisible(true);
            MultiSplitLayout.printModel(multiSplitLayout.getModel());
        });
    }
}

0 个答案:

没有答案