将对象存储在数组中并显示它们(Java)

时间:2016-12-14 18:49:17

标签: java oop jframe

我正在研究java tic tac toe游戏。首先,我为按钮创建了一个类,然后我尝试存储该类的实例数组。一切都工作正常,直到我将这些对象添加到框架。这是我的代码:

package tictactoe;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TicTacToe extends JFrame
{

    TicTacToe()
    {
        this.setLayout(null);
        this.setResizable(true);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        this.setSize(500,500);    
        this.setBackground(Color.blue);


         PlayingButton[] b = new PlayingButton [9];
        for (int i = 0 ; i < 9 ; i++)
        {
             b[i] = new PlayingButton();
        }
        b[0].setBounds(0,0,50,50);
        b[1].setBounds(50,0,50,50);
        b[2].setBounds(100,0,50,50);
        this.add(b[0]);
        this.setVisible(true);
    }


    public static void main(String[] args) 
    {
        TicTacToe board = new TicTacToe();

    }

}

导致我出问题的一行:

this.add(b[0]);

1 个答案:

答案 0 :(得分:3)

PlayingButton类应该扩展JComponent或它的子类。

相关问题