2012-04-27 69 views
1

我正在使用重绘来触发SwingWorker。根据应用程序中的自动调整选项,该工作人员自动重新创建图像以适应封闭的JPanel的宽度。暂停SwingWorker执行,直到组件停止调整大小

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

我试图避免这种情况只有​​当isDone()为真。但是我觉得有一个更好的方法来做到这一点。也许通过使用某种计时器?你有什么建议?

+1

我怀疑我的建议是否有效。虽然你可以尝试,但是你可以在你的'JPanel'中添加一个[HierarchyBoundsListener](http://docs.oracle.com/javase/7/docs/api/java/awt/event/HierarchyBoundsListener.html)你可以写''ancestorResized(...)','swingWorkerObject.cancel(true); swingWorkerObject.execute();',这可能会给人一种重新启动的感觉,不知道该解决方案有多好! ! – 2012-04-27 08:46:27

+0

+1,对于更多的注意,对于能够帮助的人,如果我从未工作:( – 2012-04-27 09:20:42

回答

1

这是一个有点左的字段,但基本上我使用的是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); 

}