当用户拖动鼠标时,收听JFrame resize事件? [英] Listen to JFrame resize events as the user drags their mouse?

查看:144
本文介绍了当用户拖动鼠标时,收听JFrame resize事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击JFrame的一角来调整大小并拖动鼠标时,JFrame会根据用户拖动时鼠标的当前位置重新绘制。你怎么能听这些事件?

When a user clicks on the corner of a JFrame to resize and drags the mouse around, the JFrame redraws based on the current position of the mouse as the user drags. How can you listen to these events?

以下是我目前的尝试:

public final class TestFrame extends JFrame {
    public TestFrame() {
        this.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                // This is only called when the user releases the mouse button.
                System.out.println("componentResized");
            }
        });
    }

    // These methods do not appear to be called at all when a JFrame
    // is being resized.
    @Override
    public void setSize(int width, int height) {
        System.out.println("setSize");
    }

    @Override
    public void setBounds(Rectangle r) {
        System.out.println("setBounds A");
    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        System.out.println("setBounds B");
    }
}

如何确定和约束用户调整大小的方式窗口(基于窗口的当前宽高比),因为它们在鼠标周围拖动?

How can I determine and constrain how the user resizes a window (based on the current aspect ratio of the window) as they are dragging around the mouse around?

推荐答案

你可能需要覆盖类似 validate (不要忘记调用super)。当然,如果您使用窗口系统配置拖动轮廓,那么仍然可能无效。

You probably need to override something like validate (don't forget to call the super). Of course, that still may not work if you are using a windowing system to configured to drag outlines.

这篇关于当用户拖动鼠标时,收听JFrame resize事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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