Java:构造函数不工作

发布于 2025-01-07 04:19:13 字数 1597 浏览 0 评论 0原文

我正在处理的 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
        }
    }
}
}

I am having a problem with a GUI constructor I am working on. It is supposed to be the GUI to a tic tac toe game, but none of my buttons are being created, and my GUI window is blank. I am really confused. I create an instance of the TicTacToePanel and add it to the main 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
        }
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛放△進行李 2025-01-14 04:19:13

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

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

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

The function you have, despite being called ButtonConstructor, is not a constructor.

In Java, a constructor must share the name of its parent class (and have no return type). The correct signature would be public TicTacToePanel().

I cannot say for sure without seeing a more complete view of your code (you have surely omitted most of it), but it is likely that you are not calling the function with which you provided us at all, but rather using the implied no-argument constructor. Try renaming the function to the signature I gave above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文