我想要一个简单的 JFrame
,其中包含 JLabel
(将图像显示为图标)和 JSlider
(在 40图片)。
当我在 slider 的 StateChange
事件上加载新图像时,程序变得非常慢,尤其是当我快速滑动时。
所以我正在考虑预加载 40 张图像并通过 slider 替换它们。这明智且可行吗?
最佳答案
我想,你有这样的东西:
public class MyClass {
// other declarations
private JLabel label;
// other methods
public void stateChange(ChangeEvent e) {
label.setIcon(new ImageIcon(...)); // here is code to determine name of the icon to load.
timer = null;
}
}
您需要更改代码如下:
public class MyClass {
// other declarations
private JLabel label;
private Timer timer; // javax.swing.Timer
// other methods
public void stateChange(ChangeEvent e) {
if (timer != null) {
timer.stop();
}
timer = new Timer(250, new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setIcon(new ImageIcon(...)); // here is code to determine name of the icon to load.
timer = null;
}
});
timer.setRepeats(false);
timer.start();
}
}
关于java - 预加载图像以用作 JLabel 图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41936377/