首页 > 解决方案 > 使用秋千设置 3x3 网格布局

问题描述

我试图用摇摆建立一个井字游戏。使用网格布局会出现奇怪的行数和列数,似乎无法达到 3x3。我究竟做错了什么?我应该只使用浮动布局并设置位置吗?

import java.awt.*;
import javax.swing.*;

public class main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Tic Tac Toe");
        JPanel panel = new JPanel();

        frame.add(panel);
    
        GridLayout grid = new GridLayout(3,3);
    
    
        frame.setVisible(true); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setLayout(grid);
        frame.setResizable(false);
    
        JButton button1 = new JButton();
        JButton button2 = new JButton();
        JButton button3 = new JButton();
        JButton button4 = new JButton();
        JButton button5 = new JButton();
        JButton button6 = new JButton();
        JButton button7 = new JButton();
        JButton button8 = new JButton();
        JButton button9 = new JButton();
    
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        frame.add(button7);
        frame.add(button8);
        frame.add(button9);
    
    }
}

标签: javaswing

解决方案


尽管您已经接受了@JustinCoding答案,但我觉得也有义务回答,因为该答案并没有真正解释为什么您的代码没有产生您想要的结果。

您的问题是您正在向JPaneltheJFrame和您JButton的 s 添加一个空。只需删除那部分代码。这是您更正的代码。我评论了需要删除的行。

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
//import javax.swing.JPanel;

public class main {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Tic Tac Toe");
//        JPanel panel = new JPanel();
//        frame.add(panel);

        GridLayout grid = new GridLayout(3, 3);

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setLayout(grid);
        frame.setResizable(false);

        JButton button1 = new JButton();
        JButton button2 = new JButton();
        JButton button3 = new JButton();
        JButton button4 = new JButton();
        JButton button5 = new JButton();
        JButton button6 = new JButton();
        JButton button7 = new JButton();
        JButton button8 = new JButton();
        JButton button9 = new JButton();

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        frame.add(button7);
        frame.add(button8);
        frame.add(button9);
    }
}

这是我运行代码时的样子。

正在运行的 Swing 应用程序的屏幕截图

解释

最初,为了向 a 添加组件JFrame,您必须首先调用getContentPane方法,该方法返回一个您可以添加组件的ContainerJFrame被称为顶级容器,其内容窗格是您添加到其中的所有组件的容器。默认内容窗格JPanel其布局管理器为BorderLayout。因此,无需将您的组件添加到 aJPanel并将其添加JPanelJFrame. 您可以直接将组件添加到JFrame. 请注意,当您将组件直接添加到 aJFrame时,您实际上是将它们添加到 a JPanelwith BorderLayout

由于内容窗格是 a JPanel,因此您还可以自由更改其布局管理器 - 正如您在代码中通过以下行所做的那样:

frame.setLayout(grid);

请注意,您只需要在GridLayout构造函数中设置其中一个维度。这是该构造函数的javadoc中的引用。

行和列中的一个(但不是两者)可以为零,这意味着可以将任意数量的对象放置在一行或一列中。

换句话说,对于您的井字游戏,您只需将列设置为 3。这意味着每行将包含不超过三列,因此当您添加九个按钮时,GridLayout将确保它们将排列成三个每行三列。因此,对于您的井字游戏板,您可以使用以下代码。

GridLayout grid = new GridLayout(0, 3); // zero rows and three columns

推荐阅读