Swing + MigLayout,动态增长和缩小其他组件

时间:2013-07-18 06:02:12

标签: java swing layout miglayout

我想实现组件划分可用的总空间,具体取决于它们是否应缩小或增长。这应该是动态发生的,意思是;我没有关于添加到屏幕的组件数量的任何信息。组件应根据其属性保持高度或增长:

+++++++++++++++++++++++++++++++++
|   +-----------------------+   |
|   |       don't grow      |   |
|   +-----------------------+   |
|                               |
|   +-----------------------+   |
|   |                       |   |
|   |       growy           |   |
|   |                       |   |
|   +-----------------------+   |
|                               |
|   +-----------------------+   |
|   |       don't grow      |   |
|   +-----------------------+   |
|                               |
|   +-----------------------+   |
|   |       don't grow      |   |
|   +-----------------------+   |
|                               |
|   +-----------------------+   |
|   |                       |   |
|   |       growy           |   |
|   |                       |   |
|   +-----------------------+   |   
|                               |
|   +-----------------------+   |
|   |       don't grow      |   |
|   +-----------------------+   |
+++++++++++++++++++++++++++++++++

我能够创建以下结果:

Almost there.. but not quite

提供以下SSCCE

public class SimpleMain {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("MigLayout");

        JPanel panel =  new JPanel(new MigLayout("debug, flowy", "grow", "grow"));

        // 1st set of textfields
        panel.add(new JTextField(), "shrink, growx, top");
        panel.add(new JTextField(), "grow, top");
        panel.add(new JTextField(), "shrink, growx, top");

        // 2nd set of textfields
        panel.add(new JTextField(), "shrink, growx, top");
        panel.add(new JTextField(), "grow, top");
        panel.add(new JTextField(), "shrink, growx, top");

        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);
    } 

}

第2和第5个文本字段按预期增长。其他的仍然是他们的尺寸,尽管他们的容器没有“收缩”,并且正在被处理以将其空间与其他组件分开。第2和第5个文本字段应占用所有“额外”空间。

1 个答案:

答案 0 :(得分:1)

经过一些测试后,我可能找到了一个符合我问题中提到的要求的解决方案。

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

private static void createAndShowGUI() {
    JFrame frame = new JFrame("MigLayout");

    JPanel panel =  new JPanel(new MigLayout(new LC().debug(1000).flowY().fillX()));

    // 1rd set of components
    panel.add(new JLabel("Titel"), new CC().grow());
    panel.add(new JTextArea(), new CC().grow().push()); // this component can use all the extra space

    // 2st set of components
    panel.add(new JLabel("Titel"), new CC().grow());
    panel.add(new JTextField(), new CC().grow());
    panel.add(new JLabel("Description"), new CC().grow());

    // 3nd set of components
    panel.add(new JLabel("progress"), new CC().grow());
    panel.add(new JProgressBar(), new CC().grow());
    panel.add(new JTextArea(), new CC().grow().push()); // this component can use all the extra space


    frame.getContentPane().add(panel);

    frame.pack();
    frame.setVisible(true);
} 

结果:

Result

您可能会注意到,屏幕上的2个组件占用了所有额外空间,而其他组件则保留为默认大小。