来回切换 Java 按钮图像?
我有一个使用开关的程序,该开关在单击按钮时将动作事件传递给它:
public void buttonImageReveal(ActionEvent e){
String temp = e.getActionCommand();
switch(temp){
case "1":
((JButton)e.getSource()).setIcon(one);
delay();
((JButton)e.getSource()).setIcon(null);
break;
延迟只是对一个等待 1 秒的函数的调用:
public void delay(){
try
{
Thread.sleep(1000);
}
catch(InterruptedException e1)
{
e1.printStackTrace();
}
}
所有结果都是等待,没有图像,所需的结果是闪烁图像一秒钟。
提前致谢!
I have a program that uses a switch which has the action event passed to it on button click:
public void buttonImageReveal(ActionEvent e){
String temp = e.getActionCommand();
switch(temp){
case "1":
((JButton)e.getSource()).setIcon(one);
delay();
((JButton)e.getSource()).setIcon(null);
break;
Delay is just a call to a function with a 1 second wait:
public void delay(){
try
{
Thread.sleep(1000);
}
catch(InterruptedException e1)
{
e1.printStackTrace();
}
}
All that results is a wait and no image, the desired outcome is a flash of the image for a second.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置图标后,您将使线程休眠,因此它无法绘制新图标。睡眠结束后立即将图标设置为空。所以你永远不会看到图标被绘制。
您可以尝试定期使用
javax.swing.Timer
更改图标。After setting the icon you are making the thread sleep so it can't paint the new icon. Immediately after sleep is over you set icon to null. So you will never see the icon painted.
You can try to change the icon using
javax.swing.Timer
periodically.