暂停 SwingWorker 执行,直到组件停止调整大小



>我正在使用重绘来触发SwingWorker。此工作线程根据应用程序中的自动调整选项自动重新创建图像以适合封闭JPanel的宽度。

问题是导致内存不足异常。这是因为图像具有较大的分辨率,并且在拖动窗口大小时会连续调用工作线程。

我试图避免这种情况的只是isDone()属实时才execute()。但是,我觉得有更好的方法可以做到这一点。也许通过使用某种计时器?你有什么建议?

这是一个有点左的字段,但基本上我所做的是使用 javax.swing.Timer。 基本思想是仅在计时器触发时执行操作。

private Timer timer;
.
.
.
timer = new Timer(250, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // Perform required action
        // Start the swing worker ??
    }
});
timer.setCoalesce(true);
timer.setRepeats(false);
.
.
.
public void setBounds(int x, int y, int width, int height) {
    // Force the time to start/reset
    timer.restart();
    super.setBounds(x, y, width, height);
}

最新更新