Java 如何在JFrame中显示正方形网格中的JButton?

Java 如何在JFrame中显示正方形网格中的JButton?,java,jframe,jbutton,Java,Jframe,Jbutton,如何在正方形形成的网格中显示按钮?我一直试着自己做,但似乎不起作用,因为JButton没有正确地显示在网格中 import javax.swing.*; import java.awt.*; import java.util.*; public class panel extends JFrame{ public panel() { super(); setSize(600,600); setLocationRelativeTo(null)

如何在正方形形成的网格中显示按钮?我一直试着自己做,但似乎不起作用,因为JButton没有正确地显示在网格中

import javax.swing.*;
import java.awt.*;
import java.util.*;
public class panel extends JFrame{
    public panel() {
        super();
        setSize(600,600);
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void paint(Graphics g) {
        /*
         * Grid
         */
        g.drawRect(200,0,0,600);
        g.drawRect(400,0,0,600);
        g.drawRect(0,200,600,0);
        g.drawRect(0,400,600,0);

        JPanel p = new JPanel();
        Dimension d = new Dimension (100,100);
        JButton b = new JButton("Button");
        b.setPreferredSize(d);

        p.add(b);
        add(p, BorderLayout.NORTH);
    }
}

使用GridLayout而不是BorderLayout以网格形式显示jbutton,
-

哦,是的,我没想过。。。谢谢