私は掃海艇ゲームに取り組んでおり、爆弾 (またはこの場合は私が作成したパンダの画像) を押したときにゲーム空間の下に表示されるようにしたいと考えています。この時点で、すべてのスペースの下に表示させたいだけです。その後、表示される場所をランダム化する方法を知っています。問題は、表示させることです。
現在、このトピックに適用される私のコードの部分は、2 つの異なるクラスにあります。
ファーストクラス
public class MSBoard extends JPanel implements ActionListener
{
int x = 8;
int y = 8;
public GridLayout gl = new GridLayout(x,y,0,0);
public MSBoxes boxarray[][] = new MSBoxes[x][y];
MSBoard()
{
super();
setLayout(gl);
for(int i=0;i<x;i++)
for (int j=0;j<y;j++)
{
boxarray[i][j] = new MSBoxes();
add(boxarray[i][j]);
}
}
public void actionPerformed(ActionEvent ae){}
}
2つ目
public class MSBoxes extends JPanel implements ActionListener
{
public JButton b1;
ImageIcon panda;
MSBoxes()
{
super();
panda = new ImageIcon("panda.gif");
b1 = new JButton();
add(b1);
b1.addActionListener(this);
b1.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(b1 == ae.getSource())
{
b1.setVisible(false);
}
}
}