将对象存储在数组中并显示它们(Java)
问题描述:
我正在开发java井字棋游戏。首先,我创建了一个按钮类,然后我尝试存储该类的实例数组。一切工作正常,直到我将这些对象添加到框架。这里是我的代码:将对象存储在数组中并显示它们(Java)
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]);
答
PlayingButton类应该扩展JComponent的或它的子类。
+0
它的工作原理,谢谢! –
然后,会发生什么?你期望代码做什么,它做什么呢? –
什么是PlayingButton?发生了什么?是否有堆栈跟踪? [我怎么问一个好问题?](http://stackoverflow.com/help/how-to-ask) – flakes
PlayingButton是我创建它的另一个类,用于从中获取对象。 –