0

大きな正方形を作る 4 つの正方形と、その下にあるボタンが正方形全体に広がるレイアウトを作成しようとしています。

正方形を完璧に作成したところを試しGridLayoutてみましたが、ボタンが画面全体に伸びませんでした。

私もやってみGridBagLayoutましたが間隔があって四角が小さくてダメでした。

PS: ボタンは、正方形の場合は button1、button2、button3、button4、下のボタンの場合は start です。

また、正方形を400x400にしたいです。

私のコードGridLayout

this.setLayout(new GridLayout(3,2));




        button1 = new JButton();
        //button1.setBackground(Color.green);
        button1.setBounds(0, 0, 200, 200);

        button2 = new JButton();
        //button2.setBounds(0, 200, 200, 200);
        button2.setBackground(Color.red);

        button3 = new JButton();
        //button3.setBackground(Color.yellow);
        button3.setBounds(200, 0, 200, 200);

        button4 = new JButton();
        //button4.setBounds(200, 200, 200, 200);
        button4.setBackground(Color.blue);

        start = new JButton("Start");
        //start.setBounds(300, 300, 100, 100);

        this.add(button1);
        this.add(button2);
        this.add(button3);
        this.add(button4);
        this.add(start);
4

2 に答える 2

2

コンポーネント レイアウトを使用してみてください... と の組み合わせによりBorderLayoutGridLayout必要な結果が得られるはずです。

ここに画像の説明を入力

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FourSquare {

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

    public FourSquare() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JPanel squarePane = new JPanel(new GridLayout(2, 2));
                JPanel controlPane = new JPanel(new BorderLayout());

                squarePane.add(new JButton("1"));
                squarePane.add(new JButton("2"));
                squarePane.add(new JButton("3"));
                squarePane.add(new JButton("4"));

                controlPane.add(squarePane);
                controlPane.add(new JButton("Bottom"), BorderLayout.SOUTH);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(controlPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}
于 2013-08-07T02:49:52.547 に答える
1

まだコメントとして質問することはできませんが、gridlayout を使用して 4 つのボタンで構成される正方形と、その下に長いボタンを配置したいのではないでしょうか?

(このような?)

[1] [2]
[3] [4]
[開始]

私にとっては、これでそれを達成します:

//assuming this code is in a class that extends JFrame
this.setLayout( new BorderLayout() );

JPanel pnlSquare = new JPanel( new GridLayout( 2 , 2 ) );
button1.setPreferredSize( new Dimension( 200 , 200 ) );
pnlSquare.add( button1 );
button2.setPreferredSize( new Dimension( 200 , 200 ) );
pnlSquare.add( button2 );
button3.setPreferredSize( new Dimension( 200 , 200 ) );
pnlSquare.add( button3 );
button4.setPreferredSize( new Dimension( 200 , 200 ) );
pnlSquare.add( button4 );
this.add( pnlSquare , BorderLayout.CENTER );

this.add( start , BorderLayout.SOUTH );

これでよろしければ教えてください。

また、これが学校のプロジェクト用である場合は、コードを説明できることを確認した方がよいでしょう。そうしないと、盗作でトラブルになる可能性があります。

于 2013-08-07T02:49:11.247 に答える