我试图在3x3网格中显示9个3x3网格。但是没有按钮出现。我不得不创建所有单独的按钮,我不知道如果我的代码中有明显错误,他们是否占用了很多空间。感谢您的任何意见!
public class BoardGui extends JFrame{
private JPanel center;
private JPanel grid00;
private JPanel grid01;
private JPanel grid02;
private JPanel grid10;
private JPanel grid11;
private JPanel grid12;
private JPanel grid20;
private JPanel grid21;
private JPanel grid22;
private JButton sq0000,sq0100,sq0200,sq1000,sq1100,
sq1200,sq2000,sq2100,sq2200;
private JButton sq0001,sq0101,sq0201,sq1001,sq1101,sq1201,sq2001
,sq2101,sq2201;
private JButton sq0002,sq0102,sq0202,sq1002,sq1102,sq1202
,sq2002,sq2102,sq2202;
private JButton sq0010,sq0110,sq0210,sq1010,sq1110,sq1210,
sq2010,sq2110,sq2210;
private JButton sq0011,sq0111,sq0211,sq1011,sq1111,
sq1211,sq2011,sq2111,sq2211;
private JButton sq0012,sq0112,sq0212,sq1012,sq1112,sq1212,
sq2012,sq2112,sq2212;
private JButton sq0020,sq0120,sq0220,sq1020,sq1120,sq1220,
sq2020,sq2120,sq2220;
private JButton sq0021,sq0121,sq0221,sq1021,sq1121,
sq1221,sq2021,sq2121,sq2221;
private JButton sq0022,sq0122,sq0222,sq1022,sq1122,sq1222,
sq2022,sq2122,sq2222;
public void Board(){
setLayout(new BorderLayout());
center.setLayout(new GridLayout(3,3));
grid00 = new JPanel();
grid01 = new JPanel();
grid02 = new JPanel();
grid10 = new JPanel();
grid11 = new JPanel();
grid12 = new JPanel();
grid20 = new JPanel();
grid21 = new JPanel();
grid22 = new JPanel();
grid00.setLayout(new GridLayout(3,3));
grid01.setLayout(new GridLayout(3,3));
grid02.setLayout(new GridLayout(3,3));
grid10.setLayout(new GridLayout(3,3));
grid11.setLayout(new GridLayout(3,3));
grid12.setLayout(new GridLayout(3,3));
grid20.setLayout(new GridLayout(3,3));
grid21.setLayout(new GridLayout(3,3));
grid22.setLayout(new GridLayout(3,3));
sq0000 = new JButton();
sq0001 = new JButton();
sq0002 = new JButton();
sq0010 = new JButton();
sq0011 = new JButton();
sq0012 = new JButton();
sq0020 = new JButton();
sq0021 = new JButton();
sq0022 = new JButton();
sq0100 = new JButton();
sq0101 = new JButton();
sq0102 = new JButton();
sq0110 = new JButton();
sq0111 = new JButton();
sq0112 = new JButton();
sq0120 = new JButton();
sq0121 = new JButton();
sq0122 = new JButton();
sq0200 = new JButton();
sq0201 = new JButton();
sq0202 = new JButton();
sq0210 = new JButton();
sq0211 = new JButton();
sq0212 = new JButton();
sq0220 = new JButton();
sq0221 = new JButton();
sq0222 = new JButton();
sq1000 = new JButton();
sq1001 = new JButton();
sq1002 = new JButton();
sq1010 = new JButton();
sq1011 = new JButton();
sq1012 = new JButton();
sq1020 = new JButton();
sq1021 = new JButton();
sq1022 = new JButton();
sq1100 = new JButton();
sq1101 = new JButton();
sq1102 = new JButton();
sq1110 = new JButton();
sq1111 = new JButton();
sq1112 = new JButton();
sq1120 = new JButton();
sq1121 = new JButton();
sq1122 = new JButton();
sq1200 = new JButton();
sq1201 = new JButton();
sq1202 = new JButton();
sq1210 = new JButton();
sq1211 = new JButton();
sq1212 = new JButton();
sq1220 = new JButton();
sq1221 = new JButton();
sq1222 = new JButton();
sq2000 = new JButton();
sq2001 = new JButton();
sq2002 = new JButton();
sq2010 = new JButton();
sq2011 = new JButton();
sq2012 = new JButton();
sq2020 = new JButton();
sq2021 = new JButton();
sq2022 = new JButton();
sq2100 = new JButton();
sq2101 = new JButton();
sq2102 = new JButton();
sq2110 = new JButton();
sq2111 = new JButton();
sq2112 = new JButton();
sq2120 = new JButton();
sq2121 = new JButton();
sq2122 = new JButton();
sq2200 = new JButton();
sq2201 = new JButton();
sq2202 = new JButton();
sq2210 = new JButton();
sq2211 = new JButton();
sq2212 = new JButton();
sq2220 = new JButton();
sq2221 = new JButton();
sq2222 = new JButton();
grid00.add(sq0000);
grid00.add(sq0001);
grid00.add(sq0002);
grid00.add(sq0010);
grid00.add(sq0011);
grid00.add(sq0012);
grid00.add(sq0020);
grid00.add(sq0021);
grid00.add(sq0022);
grid01.add(sq0100);
grid01.add(sq0101);
grid01.add(sq0102);
grid01.add(sq0110);
grid01.add(sq0111);
grid01.add(sq0112);
grid01.add(sq0120);
grid01.add(sq0121);
grid01.add(sq0122);
grid02.add(sq0200);
grid02.add(sq0201);
grid02.add(sq0202);
grid02.add(sq0210);
grid02.add(sq0211);
grid02.add(sq0212);
grid02.add(sq0220);
grid02.add(sq0221);
grid02.add(sq0222);
grid10.add(sq1000);
grid10.add(sq1001);
grid10.add(sq1002);
grid10.add(sq1010);
grid10.add(sq1011);
grid10.add(sq1012);
grid10.add(sq1020);
grid10.add(sq1021);
grid10.add(sq1022);
grid11.add(sq1100);
grid11.add(sq1101);
grid11.add(sq1102);
grid11.add(sq1110);
grid11.add(sq1111);
grid11.add(sq1112);
grid11.add(sq1120);
grid11.add(sq1121);
grid11.add(sq1122);
grid12.add(sq1200);
grid12.add(sq1201);
grid12.add(sq1202);
grid12.add(sq1210);
grid12.add(sq1211);
grid12.add(sq1212);
grid12.add(sq1220);
grid12.add(sq1221);
grid12.add(sq1222);
grid20.add(sq2000);
grid20.add(sq2001);
grid20.add(sq2002);
grid20.add(sq2010);
grid20.add(sq2011);
grid20.add(sq2012);
grid20.add(sq2020);
grid20.add(sq2021);
grid20.add(sq2022);
grid21.add(sq2100);
grid21.add(sq2101);
grid21.add(sq2102);
grid21.add(sq2110);
grid21.add(sq2111);
grid21.add(sq2112);
grid21.add(sq2120);
grid21.add(sq2121);
grid21.add(sq2122);
grid22.add(sq2200);
grid22.add(sq2201);
grid22.add(sq2202);
grid22.add(sq2210);
grid22.add(sq2211);
grid22.add(sq2212);
grid22.add(sq2220);
grid22.add(sq2221);
grid22.add(sq2222);
center.add(grid00);
center.add(grid01);
center.add(grid02);
center.add(grid10);
center.add(grid11);
center.add(grid12);
center.add(grid20);
center.add(grid21);
center.add(grid22);
add(center, BorderLayout.CENTER);
}
public static void main(String[] args) {
BoardGui gui = new BoardGui();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(700, 700);
gui.setResizable(true);
gui.setVisible(true);
gui.setTitle("Tic Tac Toe Extreme");
}
}
答案 0 :(得分:1)
到目前为止,通过查看您的代码,它将生成NullPointerException
,因为中心永远不会被初始化...至于这是否是您的实际问题,我只能猜测
话虽如此,您需要学习如何使用数组并将代码分解为可管理的块。
有很多重复,你会发疯,不要以某种方式重复使用它,例如......
您可以创建一个自定义JPanel
,它的唯一责任是显示3x3网格按钮......
public class ButtonPane extends JPanel {
private JButton[][] btns;
public ButtonPane() {
setLayout(new GridLayout(3, 3));
btns = new JButton[3][3];
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
btns[row][col] = new JButton(row + "x" + col);
add(btns[row][col]);
}
}
}
public JButton getButtonAt(int row, int col) {
return btns[row][col];
}
}
然后你可以创建另一个自定义面板,它的职责是显示这些按钮面板的3x3网格......
public class ContentPane extends JPanel {
private ButtonPane[][] buttonPanels;
public ContentPane() {
setLayout(new GridLayout(3, 3));
buttonPanels = new ButtonPane[3][3];
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttonPanels[row][col] = new ButtonPane();
add(buttonPanels[row][col]);
}
}
}
public ButtonPane getButtonPaneAt(int row, int col) {
return buttonPanels[row][col];
}
}
然后把它们放在一个框架或其他容器上......
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test{
public static void main(String[] args) {
new JavaApplication834();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ContentPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ContentPane extends JPanel {
private ButtonPane[][] buttonPanels;
public ContentPane() {
setLayout(new GridLayout(3, 3));
buttonPanels = new ButtonPane[3][3];
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttonPanels[row][col] = new ButtonPane();
add(buttonPanels[row][col]);
}
}
}
public ButtonPane getButtonPaneAt(int row, int col) {
return buttonPanels[row][col];
}
}
public class ButtonPane extends JPanel {
private JButton[][] btns;
public ButtonPane() {
setLayout(new GridLayout(3, 3));
btns = new JButton[3][3];
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
btns[row][col] = new JButton(row + "x" + col);
add(btns[row][col]);
}
}
}
public JButton getButtonAt(int row, int col) {
return btns[row][col];
}
}
}
请记住,Java是一种面向对象的语言,它是生成可重用块的目标之一......