Java 在JPanel上显示网格布局

Java 在JPanel上显示网格布局,java,swing,jpanel,Java,Swing,Jpanel,我的任务是在JPAnel上创建检查板。为此,我试图用有边界的JPanel来填充父JPanel,但由于某些原因,代码并没有给出期望的结果,也没有显示错误来调查原因。代码如下: private static class GlassView extends JFrame { private static int width = 600; private static int height = 750; public GlassView() {

我的任务是在JPAnel上创建检查板。为此,我试图用有边界的JPanel来填充父JPanel,但由于某些原因,代码并没有给出期望的结果,也没有显示错误来调查原因。代码如下:

private static class GlassView extends JFrame {

        private static int width = 600;
        private static int height = 750;

        public GlassView() {
            this.setSize(width, height);
            this.setVisible(true);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        }

        public static void workingFrame() {
            int cols = 0;
            int rows = 0;
            String frameName = "Bot World";

            WorkFrame workF = new WorkFrame(0, 0, frameName);
            wfFrame = workF.newFrame();
            wfFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            wfFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            wfFrame.setVisible(true);

            JSplitPane splitPane = new JSplitPane();
            splitPane.setSize(width, height);
            splitPane.setDividerSize(0);
            splitPane.setDividerLocation(150);
            splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);

            JPanel panelLeft = createLftPanel();
            JPanel panelRight = createRightPanel();

            splitPane.setLeftComponent(panelLeft);
            splitPane.setRightComponent(panelRight);
            wfFrame.add(splitPane);
        }
    }
以下是需要检查的panelRight的代码:

public static JPanel createRightPanel() {
        JPanel panel = new JPanel();
        int rows = 100;
        int cols = 100;
        panel.setLayout(new GridLayout(rows, cols));
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                JPanel pane = new JPanel();
                pane.add(new JTextField("both"));
                pane.setBorder(BorderFactory.createLineBorder(Color.black));
                panel.add(new JButton(""));
            }
        }
        return panel;
    }
公共静态JPanel createRightPanel(){
JPanel面板=新的JPanel();
int行=100;
int cols=100;
panel.setLayout(新的网格布局(行、列));
对于(int i=0;i
任何帮助都将不胜感激。谢谢你

好的,我(第二)猜测是,在调用

wfFrame.setVisible(true);
        frame.revalidate(); // <-- HERE
对我来说,下面的例子是:

public class Framed {
    public static void workingFrame() {
        JFrame frame = new JFrame();
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.getContentPane().add(createRightPanel(10, 10));

        frame.revalidate(); // <-- HERE
    }

    public static JPanel createRightPanel(int rows, int cols) {
        JPanel panel = new JPanel(new GridLayout(rows, cols));
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                JPanel pane = new JPanel();
                pane.setBackground((i+j)%2==0?Color.black:Color.white);
                panel.add(pane);
            }
        }
        return panel;
    }

    public static void main(String... none) throws Exception {
        workingFrame();
    }
}
公共类框架{
公共静态无效工作框架(){
JFrame=新JFrame();
setExtendedState(JFrame.MAXIMIZED_二者);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.getContentPane().add(createRightPanel(10,10));

frame.revalidate();//我的任务是在JPAnel上创建选中的棋盘-什么是选中的棋盘?你是指带有交替颜色方块的棋盘吗?请参阅:以获取基本示例。@camickr checked表示棋盘拆分为没有颜色的方块,但仍然不知道这是什么意思。“棋盘”是什么。没有具有该名称的Swing组件。创建“网格”没有任何特殊之处。您只需要一个带有GridLayout的JPanel。您可以指定网格中所需的列数,并对行使用0。然后为要添加到面板的元素数创建一个循环。当达到列大小时,组件将自动换行。因此,首先创建一个仅包含框架的示例,然后带有GridLayout和子面板的r面板。一旦工作正常,您将担心使用拆分窗格。如果您有问题,请发布适当的演示。一个“MRE”表示我们可以复制/粘贴/编译和测试代码,以查看您描述的问题。正是!!!frame.revalidate()-让一切顺利,你看,我不知道这个规则,因此一直在挣扎……这就是我得到的(如果你感兴趣的话)我想现在我需要去掉列之间的空白,就是这样。