8

何かがおかしい。右端のボタン (以下の例では「ヘルプ」というラベルが付いています) を JFrame に右揃えにし、巨大なボタンの幅を JFrame に関連付けますが、それぞれ少なくとも 180px にしようとしています。巨大なボタンの制約が機能するようになりましたが、正しい配置ではありません。

代替テキスト

gapbefore pushこの他のSOの質問のように)正しい配置が達成されたと思いましたが、わかりません。

誰でも私を助けることができますか?

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;

public class RightAlignQuestion {
    public static void main(String[] args) {
        JFrame frame = new JFrame("right align question");
        JPanel mainPanel = new JPanel();
        frame.setContentPane(mainPanel);

        mainPanel.setLayout(new MigLayout("insets 0", "[grow]", ""));

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new MigLayout("", "[][][][]", ""));
        for (int i = 0; i < 3; ++i)
            topPanel.add(new JButton("button"+i), "");
        topPanel.add(new JButton("help"), "gapbefore push, wrap");
        topPanel.add(new JButton("big button"), "span 3, grow");

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new MigLayout("",
              "[180:180:,grow][180:180:,grow]","100:"));
        bottomPanel.add(new JButton("tweedledee"), "grow");
        bottomPanel.add(new JButton("tweedledum"), "grow");

        mainPanel.add(topPanel, "grow, wrap");
        mainPanel.add(bottomPanel, "grow");
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
4

2 に答える 2

10

気にしないでください、わかりました:コンポーネントレベルではなく、列の仕様にギャップ制約が必要なようです:

    topPanel.setLayout(new MigLayout("", "[][][]push[]", ""));
于 2011-01-14T16:50:03.513 に答える
4

はるかに簡単でクリーンな方法(IMOH)は、コンポーネントの制約を使用して実行することです

topPanel.add(new JButton("help"), "push, al right, wrap");

Push は、ウィンドウが拡大するにつれてセルを押し出しますが、コンポーネントにそれ自体をセルの右側にバインドするように指示する必要があります。次のコードで上記を実現できます。

    JPanel topPanel = new JPanel();
    frame.setContentPane(topPanel);

    for (int i = 0; i < 3; ++i)
        topPanel.add(new JButton("button"+i), "");
    topPanel.add(new JButton("help"), "push, al right, wrap");


    topPanel.add(new JButton("big button"), "span 3, grow, wrap");

    topPanel.add(new JButton("tweedledee"), "span, split2,grow, w 180, h 100");
    topPanel.add(new JButton("tweedledum"), "w 180, h 100, grow");
于 2012-12-17T17:36:33.107 に答える