我正在开发一款扫雷游戏,我想让炸弹(或者在本例中是我创建的 Pandas 图像)在按下时显示在游戏空间下方。此时我只想让它显示在每个空间下,我知道如何随机化它之后显示的位置,问题是让它显示出来。
现在,适用于该主题的代码部分分为 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){}
}
第二个
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);
}
}
}
最佳答案
关于java - 如何在没有 JButton 的情况下使 ImageIcon(或任何类型的图像)可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15202080/