java - java - 如何用java中的另一个替换屏幕上的gif?
问题描述
我想打开加载中的gif 5秒,然后切换到另一个gif,但是我无法切换
我尝试了 frame.dispose()、Panel.removeAll() 和许多其他函数
setSize(1366, 768);
setLocation(0, 0);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel p = new JPanel();
ImageIcon image = new ImageIcon("C:\\Users\\user\\Desktop\\hmi-loading.gif");
JLabel imagelabel = new JLabel(image);
imagelabel.setBackground(Color.BLACK);
imagelabel.setForeground(Color.BLACK);
p.setBackground(Color.BLACK);
p.add(imagelabel);
getContentPane().add(p);
p.removeAll();
frame.removeAll();
p.setBackground(Color.BLACK);
ImageIcon imagee = new ImageIcon("C:\\Users\\user\\Desktop\\kAi.gif");
JLabel imagelabel1 = new JLabel(image);
imagelabel1.setBackground(Color.BLACK);
imagelabel1.setForeground(Color.BLACK);
p.setBackground(Color.BLACK);
p.add(imagelabel1);
getContentPane().add(p);
那是一个 gif .. 我如何添加一个计时器以便这个计时器被另一个替换?
我希望 gif 被另一个替换。gif打开,仅此而已。
解决方案
只是一个基于Swing Timer API 的简单示例。
public class ImageView extends JFrame {
private JPanel panel;
private JLabel label;
private Timer timer;
public ImageView() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(640, 480);
panel = new JPanel(new BorderLayout());
label = new JLabel(new ImageIcon("C:\\Users\\user\\Desktop\\hmi-loading.gif"));
panel.add(label);
timer = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
label.setIcon(new ImageIcon("C:\\Users\\user\\Desktop\\kAi.gif"));
}
});
timer.start();
this.getContentPane().add(panel);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ImageView());
}
}
我现在没有足够的时间来测试它,但是这个最小的片段应该可以工作。
推荐阅读
- php - 我正在尝试使用 php 和 mysql OOP 向数据库添加记录
- javascript - 如何将循环/递归与承诺链一起使用?
- php - 阻止在 WooCommerce 中注册的某些域
- css - 如果在下拉菜单中指定 %,为什么动画不再流畅?
- python - 将每个月有一列的文件转换为每月只有一列和一行的文件
- vue.js - 尝试使用 vee-validate 时验证不存在的字段错误
- node.js - 将多节点 API 服务器连接到单实例 Mongodb
- c# - Xamarin 表单状态容器视图不尊重布局选项
- python - 如何使用枚举在python中对字典进行排序?
- php - 如何检查以下系统中的反向关系