java - 除非我单击窗口标题,否则 JLabel 无法正常工作
问题描述
我有一个程序,我试图显示一个图像,5 秒后它从 JLabel 中删除图像,然后显示第二个图像,但第二个图像不会自行显示,除非我单击顶部的标题或栏程序创建的窗口。我想在不与窗口交互的情况下自动显示图像。有什么帮助吗?
public void pictures(){
try{
BufferedImage myPicture = ImageIO.read(new File("round-1.jpg"));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
picLabel.setSize(800,600);
add(picLabel);
picLabel.setVisible(true);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run(){
picLabel.setVisible(false);
pictures2();
}
}, 5000);
}
catch(IOException e){
e.printStackTrace();
}
}
public void pictures2(){
try{
BufferedImage myPicture2 = ImageIO.read(new File("go.png"));
JLabel picLabel2 = new JLabel(new ImageIcon(myPicture2));
picLabel2.setSize(800,600);
add(picLabel2);
picLabel2.setVisible(true);
}
catch(IOException e){
e.printStackTrace();
}
}
解决方案
也许是因为您的计时器任务的工作(运行方法)在单独的线程中运行,而不是在 Swing 事件处理线程中?
我怀疑您应该调用SwingUtilities.invokeLater
计时器的 run 方法,以便在 Swing 事件处理线程中完成对标签的更改。
推荐阅读
- eclipse - Milo OPCUA 客户端等待安全通道超时
- apostrophe-cms - Apostrophe UI 中的多列小部件选择
- c# - Jenkins 只构建改变而不是整个解决方案的项目
- javascript - ng-model 值中的 AngularJS 日期未传递给控制器
- themes - 在 CentOS 中,Virtualbox 无缝模式如何去除 Windows 上的阴影效果?
- python - 如何在条形图中每个条形顶部的 y 变量的值?
- c# - 如何将数据发布到asp.net核心后面的剃须刀页面代码
- msbuild - 改变 usql msbuild 目标
- selenium-webdriver - 如何找到父元素如果我不知道子元素和父元素之间存在的元素数量
- microservices - Eureka 和 Feign MicroWebservice 无法启动