简体   繁体   中英

How do I make an ImageIcon (or any kind of image really) visible without a JButton?

I'm working on a minesweeper game and I want to make the bomb (or in this case a panda image I created) to show up under the game space when it's pressed. At this point I just want to make it show up under every single space, I know how to do the randomizing of where it shows up after that, the problem is making it show up.

Right now the parts of my code that apply to this topic are in 2 different classes:

1st class

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){}
}

2nd one

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);
           }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM