java - 预加载图像以用作 JLabel 图标

标签 java image swing jframe jslider

我想要一个简单的 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/

相关文章:

java - 如何使用 Java 中的 JRecord 识别抄写本中字段的级别?

php - 如何使用 PHP 在图像上显示热图?

java - 移动鼠标时 JPanel FPS 下降

java - JSON 和内存问题

java - 无法让自定义监听器显示在 JMeter 的“添加”->“监听器”菜单中

java - 使用 Twitter4j API 获取 friend 的时间线

html - 具有悬停效果的图像出现在标题上方时

python - 在python中的图像中的表格上创建边框

java - JToggleButton 不绘画

java - 仅当适合视口(viewport)时才启用 JTable 的自动调整大小