How to update this image as a int variable increases
I have this JLabel that I display an image in. In another JPanel I have an int incorrectGuesses that gets added to as the user incorrectly guesses. Is there any way that I can have the image change and go through the array as the incorrectGuesses int goes up?
Right now it grabs what it is when the program first starts and never changes.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
public class leftPanel extends JPanel
{
ImageIcon[] image = new ImageIcon[13开发者_StackOverflow];
JLabel hangmanPic;
public leftPanel()
{
this.setLayout(new BorderLayout());
hangmanPic = new JLabel();
for(int i = 0; i<13; i++)
{
image[i] = new ImageIcon("Images/hangman-"+(i+1)+".jpg");
}
hangmanPic.setIcon(image[RightPanel.incorrectGuesses]);
this.add(hangmanPic, BorderLayout.CENTER);
}
}
You will have to call hangmanPic.setIcon(image[RightPanel.incorrectGuesses]);
when RightPanel.incorrectGuesses
change.
call
this.validate();
this.repaint()
after this.add(hangmanPic, BorderLayout.CENTER);
精彩评论