Java:构造函数不工作

Java:构造函数不工作,java,constructor,Java,Constructor,我正在使用的GUI构造函数有问题。它应该是tic-tac-toe游戏的GUI,但是我的所有按钮都没有被创建,而且我的GUI窗口是空白的。我真的很困惑。我创建了一个TicTacToePanel的实例,并将其添加到主JFrame中 class TicTacToePanel extends JPanel implements ActionListener { public void actionPerformed(ActionEvent e) { } //Creates the button arr

我正在使用的GUI构造函数有问题。它应该是tic-tac-toe游戏的GUI,但是我的所有按钮都没有被创建,而且我的GUI窗口是空白的。我真的很困惑。我创建了一个TicTacToePanel的实例,并将其添加到主JFrame中

class TicTacToePanel extends JPanel implements ActionListener {

public void actionPerformed(ActionEvent e) {
}
//Creates the button array using the TicTacToeCell constructor
private TicTacToeCell[] buttons = new TicTacToeCell[9];
//(6) this constructor sets a 3 by 3 GridLayout manager in the panel
///then creates the 9 buttons in the buttons arrray and adds them to the panel

//As each button is created
///The constructer is passed a row and column position
///The button is placed in both the buttons array and in the panels GridLayout
///THen an actionListener this for the button
public void ButtonConstructor() {
    //creates the layout to pass to the panel
    GridLayout mainLayout = new GridLayout(3, 3);
    //Sets a 3 by 3 GridLayout manager in the panel
    this.setLayout(mainLayout);
    int q = 1; //provides a counter when creating the buttons
    for (int row = 0; row < 3; row++) //adds to the current row
    {
        for (int col = 0; col < 3; col++) //navigates to the correct columns
        {
            System.out.println("Button " + q + " created");
            buttons[q] = new TicTacToeCell(row, col);
            mainLayout.addLayoutComponent("Button " + q, this);
            this.add(buttons[q]); //adds the buttons to the ticTacToePanel
            buttons[q].addActionListener(this); //this sets the panel's action listener to the button
            q++; //increments the counter
        }
    }
}
}
类TicTacToePanel扩展JPanel实现ActionListener{
已执行的公共无效操作(操作事件e){
}
//使用TictoeCell构造函数创建按钮数组
私有TictoeCell[]按钮=新TictoeCell[9];
//(6) 此构造函数在面板中设置一个3乘3的GridLayout管理器
///然后在按钮阵列中创建9个按钮,并将它们添加到面板中
//创建每个按钮时
///将向构造函数传递一个行和列位置
///按钮放置在按钮阵列和面板GridLayout中
///然后,一个actionListener为该按钮执行此操作
public void按钮构造函数(){
//创建要传递到面板的布局
GridLayout mainLayout=新的GridLayout(3,3);
//在面板中设置3 x 3的GridLayout管理器
此.setLayout(主布局);
int q=1;//在创建按钮时提供计数器
for(int row=0;row<3;row++)//添加到当前行
{
for(int col=0;col<3;col++)//导航到正确的列
{
System.out.println(“按钮”+q+“已创建”);
按钮[q]=新的TictoeCell(行、列);
mainLayout.addLayoutComponent(“按钮”+q,this);
this.add(buttons[q]);//将按钮添加到ticTacToePanel
按钮[q].addActionListener(this);//这将面板的操作侦听器设置为按钮
q++;//递增计数器
}
}
}
}

您拥有的函数,尽管被称为
ButtonConstructor
,但不是构造函数

在Java中,构造函数必须共享其父类的名称(并且没有返回类型)。正确的签名应该是
public TicTacToePanel()


如果没有看到代码的更完整视图(您肯定省略了其中的大部分),我不能肯定,但很可能您根本没有调用您提供给我们的函数,而是使用了隐含的无参数构造函数。尝试将函数重命名为我上面给出的签名。

找不到任何调用按钮创建例程的
TicTacToePanel
构造函数!!
TicTacToeCell
在哪里定义,它做什么?更多的代码会有所帮助。仅仅因为您将某个东西命名为“ButtonConstructor”,并不意味着它就是一个构造函数。所以不要称它为构造函数(这只会让事情变得混乱);它只是您必须显式调用的任何旧方法。您没有包含足够的相关信息或代码,也没有描述问题的性质,因此不需要太多帮助。问题在哪里。。?