java - 使用秋千设置 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);
}
}
解决方案
尽管您已经接受了@JustinCoding答案,但我觉得也有义务回答,因为该答案并没有真正解释为什么您的代码没有产生您想要的结果。
您的问题是您正在向JPanel
theJFrame
和您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);
}
}
这是我运行代码时的样子。
解释
最初,为了向 a 添加组件JFrame
,您必须首先调用getContentPane方法,该方法返回一个您可以添加组件的Container。JFrame
被称为顶级容器,其内容窗格是您添加到其中的所有组件的容器。默认内容窗格是JPanel
其布局管理器为BorderLayout。因此,无需将您的组件添加到 aJPanel
并将其添加JPanel
到JFrame
. 您可以直接将组件添加到JFrame
. 请注意,当您将组件直接添加到 aJFrame
时,您实际上是将它们添加到 a JPanel
with BorderLayout
。
由于内容窗格是 a JPanel
,因此您还可以自由更改其布局管理器 - 正如您在代码中通过以下行所做的那样:
frame.setLayout(grid);
请注意,您只需要在GridLayout
构造函数中设置其中一个维度。这是该构造函数的javadoc中的引用。
行和列中的一个(但不是两者)可以为零,这意味着可以将任意数量的对象放置在一行或一列中。
换句话说,对于您的井字游戏,您只需将列设置为 3。这意味着每行将包含不超过三列,因此当您添加九个按钮时,GridLayout
将确保它们将排列成三个每行三列。因此,对于您的井字游戏板,您可以使用以下代码。
GridLayout grid = new GridLayout(0, 3); // zero rows and three columns
推荐阅读
- angular - 使用 angular8 在 api 请求中发送下载的文件
- c - 为什么 BSON 不使用 uint32_t 而不是 int32_t 作为文档长度
- c++ - 在 C++ 中禁用检查语句
- javascript - TypeScript 错误 - 类型 'Ticket[] | undefined' 不是数组类型
- django - 如何通过单击按钮创建模型实例?
- agda - 在 Agda hello-world 中运行的未解决的元数据
- css - 将 sidebarPanel 高度设置为闪亮中 mainPanel 内容的高度
- c# - 使用 C# 更改按钮背景激活的 MainWindow
- css - 加载时没有样式的 Svelte 库
- firebase - 使用 Firebase 人脸检测比较 2 个人脸