0

jframe の jlabel 内の jbuttons に問題があります。

私はマインスイーパのようなゲームを作ろうとしていますが、ここではたくさんの jbutton が必要です。この場合は 225 あります。文字列にボタンを設定し、for & if ステートメントを追加します。問題は、プログラムをコンパイルして実行すると、最初は jbuttons が適切に整列されていないことですが、マウスを使用して jframe のサイズをどちらかの方向に変更すると、ボタンが突然整列します。

リサイズ前

ここに画像の説明を入力

リサイズ後

ここに画像の説明を入力

public class Minesweeper {

    public final void initUI() {
        JFrame frame = new JFrame("Minesweeper");
        frame.setSize(320, 300);
        frame.setBackground(Color.WHITE);
        frame.setVisible(true);
        frame.setLayout(new GridLayout(1, 1, 0, 0));
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(15, 15, 0, 0));
        frame.add(panel);    
        String[] buttons = {
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", 
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", 
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", 
            "", "", "", "", "", 
        };    

        for (int i = 0; i < buttons.length;) {    
            if (i < buttons.length) {
                panel.add(new JButton(buttons[i]));
                i++;
            } else {
                System.out.println("Unknown error 100");
            }
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {    
         Minesweeper ex = new Minesweeper();
         ex.initUI();                
    }
}
4

0 に答える 0