简体   繁体   中英

Change JButton ImageIcon on Click

I am attempting to create a JFrame Application in java that is similar to Minesweeper but with slightly different rules/goals.

I have created a grid of JButtons, 12x12 and have a JButton 2D array.

I'm trying to change the image of a button when it is clicked (make it an X or an image of a gold nugget). I know how to do this if I had an individual name for each button, but it seems not logical to create 144 individual buttons and name each of them.

So what I need to do is on the click event of that button, change/set the image of that button, but in my action listener I can only figure it out if I know the specific array coordinates of that button.

My question is how do I change the Image of that specific button? Or how do I get the values of the button[?][?] so I can change the image of that button?

Thanks!

 public class GoldPanel extends JPanel{

ImageIcon xImage = new ImageIcon("x.png");
ImageIcon goldImage = new ImageIcon("");

losingButtonListener loseButton = new losingButtonListener();
winningButtonListener winButton = new winningButtonListener();

JButton[][] button = new JButton[12][12];

//creates the layout
GridLayout layout = new GridLayout(12,12);

Random myRand = new Random();

public GoldPanel(){


    //creates panel for name/title/score/etc
    JPanel titlePanel = new JPanel();
    add(titlePanel);

    JLabel title = new JLabel("Welcome to the Goldmine Game!");
    titlePanel.add(title);

    //creates panel for the game board
    JPanel gamePanel = new JPanel();
    add(gamePanel);
    gamePanel.setLayout(layout);


    for(int i=0;i<12;i++)
    {
         for(int j=0;j<12;j++)
         {
            button[i][j] = new JButton("  ");
            gamePanel.add(button[i][j]);
            button[i][j].addActionListener(loseButton);
         }

    }

    button[0][0].addActionListener(winButton);

}//end constuctor

private class losingButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub



    }//actionPerformed 

}//buttonListener

private class winningButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        System.out.println("you win");



    }//actionPerformed 

}//winningButtonListener


 }//end GoldPanel class

If you look at the ActionEvent Documentation Page you see that every action event is constructed with an Object source . This means that if the system registers a click on the button this button gets passed to the constructor of the ActionEvent as source .

So you're actually getting the right button by casting this object to the right class.

[...]
public void actionPerformed(ActionEvent ae) {
    JButton theRightButton = (JButton) ae.getSource();
    // do stuff with the button...
}
[...]

Use a toggle button

http://docs.oracle.com/javase/7/docs/api/javax/swing/JToggleButton.html

So swing keeps track of the state for you in the button model.

Extend the button to pass x,y coord in the constructor and store them as field

Attach the same event listener to all buttons, cast source to button class and retrieve x/y of the button having been clicked

Change on the fly the pressed icon to mine or number in the event

Call click to all neighbouring empty fields if you want to replicate this rule, but make sure to check the pressed state so you don't loop back over already processed neighbours.

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