7

MigLayoutに精通している人への質問

申し訳ありませんが、質問にもっと適切な名前を考えることができませんでした...

次のようなレイアウトを作成しようとしています。

+---------+---------+
|  btn1   |  btn2   |
+---------+---------+
|                   |
|       btn3        |
|                   |
+-------------------+

ウィンドウのサイズが変更されると、コンポーネントbtn1とbtn2はx軸(それぞれ半分)を埋め、コンポーネントbtn3はx軸とy軸のすべての使用可能なスペースを埋める必要があります。

どのようにこれを達成しますか?

開始するコードは次のとおりです。

public static void main(String[] args)
{
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cp = window.getContentPane();

    cp.setLayout(new MigLayout(""));
    cp.add(new JButton("btn1"), "");
    cp.add(new JButton("btn2"), "");
    cp.add(new JButton("btn3"), "");

    window.pack();
    window.setVisible(true);
}
4

3 に答える 3

26

これはMigLayoutでは非常に簡単です。

setLayout(new MigLayout("fill"));

add(new JButton("button 1"), "w 50%");
add(new JButton("button 2"), "w 50%, wrap");
add(new JButton("button 3"), "grow, push, span");

pstantonの元の質問を読んだ場合、必要なレイアウトの説明は、彼がそれを作成した方法に非常に近いと思います。それがMigLayoutの好きなところです:)

于 2010-01-25T15:15:39.063 に答える
2

miglayoutを使用したことはありませんが、次のようになります。

...
cp.add(new JButton("btn1"));
cp.add(new JButton("btn2"), "wrap");
cp.add(new JButton("btn3"), "span");
...
于 2010-01-12T23:49:39.880 に答える
-1

だからあなたはこのようなものが欲しいですか:

サンプル画像

まさにSwingLayoutDemoには、「FlowDirection」の下にあります

そのサンプルのコードは次のとおりです。

JTabbedPane tabbedPane = new JTabbedPane();

tabbedPane.addTab("Layout: flowx, Cell: flowx", createFlowPanel("", "flowx"));
tabbedPane.addTab("Layout: flowx, Cell: flowy", createFlowPanel("", "flowy"));
tabbedPane.addTab("Layout: flowy, Cell: flowx", createFlowPanel("flowy", "flowx"));
tabbedPane.addTab("Layout: flowy, Cell: flowy", createFlowPanel("flowy", "flowy"));

public JPanel createFlowPanel(String gridFlow, String cellFlow) {
    MigLayout lm = new MigLayout("center, wrap 3," + gridFlow,
                                 "[110,fill]",
                                 "[110,fill]");

    JPanel panel = createTabPanel(lm);

    for (int i = 0; i < 9; i++) {
        JButton b = createButton("" + (i + 1));
        b.setFont(b.getFont().deriveFont(20f));
        panel.add(b, cellFlow);
    }

    JButton b = createButton("5:2");
    b.setFont(b.getFont().deriveFont(20f));
    panel.add(b, cellFlow + ",cell 1 1");

    return panel;
}
于 2010-01-12T23:57:47.743 に答える