Java重绘图像
我的脚本有问题;我想在按下按钮时重新绘制一个新图像(显示另一个图像),但该按钮没有执行任何操作......
ActionListener one = new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel2.revalidate();
panel2.repaint();
}
};
btn1.addActionListener(one);
JLabel test1 = new JLabel(myDeckOfCards.giveCardPlayer1().getImage());
panel2.add(lab1);
panel2.add(test1);
panel2.add(pn5);
panel2.add(pn1);
panel2.add(btn1);
I have a problem with my script; I want repaint a new image (another one is shown) when a button is pressed, but the button doesn't do anything...
ActionListener one = new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel2.revalidate();
panel2.repaint();
}
};
btn1.addActionListener(one);
JLabel test1 = new JLabel(myDeckOfCards.giveCardPlayer1().getImage());
panel2.add(lab1);
panel2.add(test1);
panel2.add(pn5);
panel2.add(pn1);
panel2.add(btn1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
actionPerformed
中,您需要获取JLabel
并对其调用setIcon()
,传入新图像。有几种方法可以获取 JLabel,一种是确保声明了一个
final
变量以将其包含在actionPerformed
方法范围内的某个位置,另一种是查找从panel2
内部(不推荐)。如果您为此目的声明了一个成熟的类,您还可以通过构造函数将其传递给您的
ActionListener
。编辑:
Inside
actionPerformed
you need to get hold of yourJLabel
and callsetIcon()
on it, passing in the new image.There's a few ways to get the JLabel, one is to make sure you have a
final
variable declared to contain it somewhere in scope of youractionPerformed
method, and another is to find it from inside thepanel2
(not recommended).You could also pass it in to your
ActionListener
through a constructor if you declare a full-fledged class for that purpose.EDIT: