在用户拖动鼠标时收听 JFrame 调整大小事件? [英] Listen to JFrame resize events as the user drags their mouse?

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

问题描述

当用户单击 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?

以下是我目前尝试过的:

Below is the what I have currently tried:

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 调整大小事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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