请举例说明如何使用MigLayout模拟BorderLayout?

时间:2012-10-04 17:15:16

标签: java swing layout-manager miglayout border-layout

我希望布局的WESTEAST分数具有恒定的宽度。如何使用MigLayout进行设置?

更新

以下代码不会像边框布局那样调整大小:

package testing.Test_MigLayout_01;

import javax.swing.JButton;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

public class App201210042244 
{

private class JFrame extends javax.swing.JFrame {

    {
        setLayout(new MigLayout());

        add(new JButton("1"), "dock east, width 120");
        add(new JButton("2"), "west");
        add(new JButton("3"), "center");
        add(new JButton("4"), "north");
        add(new JButton("5"), "south");

    }

}

public App201210042244() {
     SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JFrame jFrame = new JFrame();
                jFrame.pack();
                jFrame.setVisible(true);

            }
        });
}


public static void main( String[] args )
{
   new App201210042244();
}
}

窗口内部的大小保持不变:

enter image description here

1 个答案:

答案 0 :(得分:3)

panel.add(new JButton("1"), "dock east, width 120");
panel.add(new JButton("2"), "west");
panel.add(new JButton("3"), "center");
panel.add(new JButton("4"), "north");
panel.add(new JButton("5"), "south");

dock 关键字可以省略。

更新:

如果要在调整窗口大小时展开 all 所有组件:

panel.setLayout(new MigLayout("fill"));
panel.add(new JButton("1"), "dock east, width 120");
panel.add(new JButton("2"), "west");
panel.add(new JButton("3"), "center, grow");
panel.add(new JButton("4"), "north");
panel.add(new JButton("5"), "south");