Java中的Component的componentResized事件,但只能在鼠标释放时执行 [英] componentResized event for Component in Java, but only execute when mouse released

查看:1487
本文介绍了Java中的Component的componentResized事件,但只能在鼠标释放时执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的一个组件(Canvas)被调整大小时,我需要做一些计算。不幸的是,计算可能需要几百毫秒,这导致调整大小在完成时滞后。我想通过在调整大小结束时进行计算来解决这个问题(我猜当鼠标按钮被释放时)。我该如何实现呢?到目前为止,我只有以下内容:

I need to do some calculations when one of my Components (a Canvas) gets resized. Unfortunately the calculations can take a few hundred milliseconds which causes the resize to lag heavily while being done. I'd like to solve that by only doing the calculation when the resizing ended (I guess when the mouse button gets released). How can I achieve that? So far I only have the following:

MyComponent.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        super.componentResized(e);
        // some calculation
    }       
});

谢谢。

PS:我知道对于JFrame,调整大小的事件只有在鼠标按钮释放后才会触发,但不幸的是我无法将我的组件放入JFrame或扩展JFrame。

PS: I know that for a JFrame the resized event gets fired only after the mouse button is released, but unfortunately I cannot put my Component into a JFrame or having it extend a JFrame.

推荐答案

然后你可以启动javax.swing.Timer一些延迟和调整大小只重新启动计时器
,并通过调用Action或AbstractAction可以计算任何东西并输出到
GUI将在EDT

then you can start javax.swing.Timer with some delay and on resize only restart Timer and by invoking Action or AbstractAction you can calculete anything and output to the GUI will be on EDT

这篇关于Java中的Component的componentResized事件,但只能在鼠标释放时执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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