我是创建GUI的新手,我无法在JLabel上刷新图像。我已经从有相同问题的人那里读到了其他问题,但没有一个答案对我有帮助。我有一个程序,每次点击JButton时都会掷骰子,如果结果是一个,我想要改变JLabel的图像。这是我的GUI类的构造函数:
private Panel panel;
private Label label;
private TextField text;
private JButton roll;
private ArrayList<ImageIcon> deck;
private ArrayList<ImageIcon> discard;
private JLabel pic;
private JFrame f;
private ImageIcon now;
public Planechase()
{
f = new JFrame("Planechase");
deck = new ArrayList<ImageIcon>();
populate();
Collections.shuffle(deck);
discard = new ArrayList<ImageIcon>();
label = new Label("Planechase");
text = new TextField(":)",8);
text.setEditable(false);
roll = new JButton("Roll");
roll.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
int i = (int)(Math.random()*6)+1;
System.out.println(i);
if(i==1)
{
text.setText("Planeswalk");
discard.add(now);
now = deck.remove(0);
pic = new JLabel(now);
pic.updateUI();
}
else if(i==6)
{
text.setText("Chaos");
}
else
{
text.setText("Blank");
}
}
});
now = deck.remove(0);
pic = new JLabel(now);
f.getContentPane().setLayout(new FlowLayout());
f.getContentPane().add(pic);
f.getContentPane().add(text);
f.getContentPane().add(roll);
}
我尝试使用上面的updateUI(),就像在类似的问题中所建议的那样,但图片并没有改变。我还应该尝试什么?