当用户完成调整JFrame的大小时获得通知 [英] Get notified when the user finishes resizing a JFrame

查看:100
本文介绍了当用户完成调整JFrame的大小时获得通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JFrame中有一个分形生成组件(JPanel的子类).当用户调整窗口大小时,需要花费相当长的时间才能将分形更新为新的大小.

I have a fractal generation component (a subclass of JPanel) inside a JFrame. When the user resizes the window, it takes quite a while to update the fractal to the new size.

我目前在JPanel上有一个ComponentListener,但是每次用户在拖动窗口边框时移动鼠标时都会调用它的componentResized事件.这意味着分形被告知要多次调整大小,然后(在几分钟的过程中)缓慢地增长到新的大小.

I currently have a ComponentListener on the JPanel, but its componentResized event is called every time the user moves the mouse while dragging the window border. This means that the fractal is told to resize many times, and slowly (over the course of a few minutes) grows to the new size.

当用户松开鼠标键时,是否有一种通知方式,以便我只能在用户调整大小后更改分形大小?

Is there a way to be notified when the user releases the mouse button, so that I can only change the fractal size when the user has finished resizing?

其他人已经报告了这种情况是在将侦听器附加到JFrame时发生的,但这没有.为我工作(以及其他) ,由于某种原因.

Others have reported this happening when the listener is attached to the JFrame instead, but this doesn't work for me (and others), for some reason.

推荐答案

除了每次接收到接收事件都开始计算之外,您只能在接收到最后一个事件后才使用计时器启动计算,例如用伪代码(或者至少是我直接在此处而不是在我的IDE中键入的代码)

Instead of starting the calculation each time you receive a receive-event, you can only start the calculation after you received the last event by using a timer, e.g. in pseudo-code (or at least code which I typed here directly and not in my IDE)

private Timer recalculateTimer = new Timer( 20, myRecalculateActionListener );

constructor(){
  recalculateTimer.setRepeats( false );
}
@Override
public void componentResized(ComponentEvent e){
  if ( recalculateTimer.isRunning() ){
    recalculateTimer.restart();
  } else {
    recalculateTimer.start();
  }
}

您仍然可以将其与安德鲁斯的建议结合使用,使用拉伸后的图像,直到计算完成为止.

And you can still combine this with Andrews suggestion to use an image which you stretch until the calculation has actually finished.

这篇关于当用户完成调整JFrame的大小时获得通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆