简体   繁体   中英

remove image icon in jbutton

I have a selectpic() method to set images in the jbuttons.

public void selectpic() {
    rule rule = new rule();
    rule.shuffle();
    for (int i = 0; i < 9; i++) {
        if (rule.pic[i] == 0) {
            Icon0.setImage(Icon0.getImage().getScaledInstance(WIDTH,HEIGHT,Image.SCALE_DEFAULT));
            panel1.add(new JButton(Icon0));
        } else if (rule.pic[i] == 1) {
            Icon1.setImage(Icon1.getImage().getScaledInstance(WIDTH,HEIGHT,Image.SCALE_DEFAULT));
            panel1.add(new JButton(Icon1));
        } else if (rule.pic[i] == 2) {
            Icon2.setImage(Icon2.getImage().getScaledInstance(WIDTH,HEIGHT,Image.SCALE_DEFAULT));
            panel1.add(new JButton(Icon2));
        } else if (rule.pic[i] == 3) {
            Icon3.setImage(Icon3.getImage().getScaledInstance(WIDTH,HEIGHT,Image.SCALE_DEFAULT));
            panel1.add(new JButton(Icon3));
        } else if (rule.pic[i] == 4) {
            Icon4.setImage(Icon4.getImage().getScaledInstance(WIDTH,HEIGHT,Image.SCALE_DEFAULT));
            panel1.add(new JButton(Icon4));
        } else if (rule.pic[i] == 5) {
            Icon5.setImage(Icon5.getImage().getScaledInstance(WIDTH,HEIGHT,Image.SCALE_DEFAULT));
            panel1.add(new JButton(Icon5));
        } 
    }
}

When each round is finished i have to call the selectpic() method again to change images.

However, i can't just simply call that method again to do it.

Q1. Is it necessary to remove the images first and then call the selectpic() again, so that it can change the images?

Q2. If so, i searched in forum and found using

xxxx.setImage(null); 

can remove the image(xxxx is the name of that jbutton),

but in the selectpic(), i use this to add the jbuttons.

panel1.add(new JButton(Icon0));

how can i know the name of the jbuttons so that i can use xxxx.setImage(null); to remove the images first?

Thanks!!

You're not keeping track of your JButtons, and calling selectpic() fills the JPanel with buttons, so calling it multiple times will create a lot of buttons.

I recommend you to have your JButtons in the class fields, like this:

private JButton button1;

Then, when you initialize your UI, initialize your buttons too:

// UI init here
button1 = new JButton();
panel1.add(button1);

And in the selectpic method, set your icons.

button1.setIcon(choosedIcon);

PD: Now I look more carefully to your code, an array of JButtons would be better, I think. So, the field declaration would be this:

private JButton[] buttons = new JButton[numberOfButtons];

The initalization would be something like this:

for (int i = 0; i < numberOfButtons; i++) {
    buttons[i] = new JButton();
    panel1.add(buttons[i]);
}

And for setting the icon,

buttons[i].setIcon(chosedIcon);

PD2: I recommend you to start your variable names with a lowercase letter, like in the Naming Convention .

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