SWT 鼠标滚轮监听器

发布于 2024-12-11 10:14:30 字数 1128 浏览 0 评论 0原文

我已经对此进行了一段时间的研究,并且发现有关 SWT MouseWheelListeners 的信息很少。看看你们中是否有人遇到过这种情况,或者是否有可以帮助解释这些问题的链接。

我正在尝试查找有关 SWT MouseWheelListener 的信息以及如何正确使用它。我正在尝试使用 MouseWheelListener 在复合材料上创建缩放效果,在其上绘制多个复合对象。

本质上,当向上滚动放大时,通过以正常大小的两倍重新绘制画布,以比例布局重新绘制画布上的对象,并将焦点移动到滚动的点。

我的问题如下: 是否可以在 Composite 上使用 MouseWheelListener ,或者侦听器仅用于滚动复合等对象(我知道该方法就在那里;当我尝试在对象上滚动时(包括在调试时)什么也没有发生)? 如果可能的话,如何在 Composite 上启动 MouseScrolledEvent ? 如何区分滚轮向上和向下(e.count 向上为正,向下为负)?

代码如下:

public TagCloudComposite(Composite parent, int style) {
    super(parent, style);
    addMouseWheelListener(new MouseWheelListener() {
        public void mouseScrolled(MouseEvent e) {
            int count = e.count;
            System.out.println(count);
            // int direction = (Math.abs(count) > 0) ? UP : DOWN;
            // changeBackground(direction);
        }
    });
    this.setLayout(new FillLayout());
    this.setMinWeight(1);
    this.setMaxWeight(100);
    c = new Composite(this, SWT.NONE);
    this.setSize(300, 200);
}

I have done research into this for a while now and have found very little information on SWT MouseWheelListeners. Looking to see if any of you have encountered this or have a link to something that could help explain them.

I am trying to find out information on the SWT MouseWheelListener and how it is used appropriately. I am attempting to use the MouseWheelListener to create a zoom effect on a composite which draws multiple composite objects on it.

In essence, when wheeling up zoom in by redrawing the canvas at twice normal size, repainting objects on the canvas in a proportional layout, and moving the focus to the point wheeled on.

My questions are the following:
Is it possible to use a MouseWheelListener on a Composite or is the listener only for objects like scrolled composites (I know the method is there; nothing is happening when I attempt to scroll on my object (including at debug)?
How to kick off a MouseScrolledEvent on a Composite if possible?
How to differentiate between wheel up and wheel down (e.count is positive for up & negative for down)?

Code follows:

public TagCloudComposite(Composite parent, int style) {
    super(parent, style);
    addMouseWheelListener(new MouseWheelListener() {
        public void mouseScrolled(MouseEvent e) {
            int count = e.count;
            System.out.println(count);
            // int direction = (Math.abs(count) > 0) ? UP : DOWN;
            // changeBackground(direction);
        }
    });
    this.setLayout(new FillLayout());
    this.setMinWeight(1);
    this.setMaxWeight(100);
    c = new Composite(this, SWT.NONE);
    this.setSize(300, 200);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声静候 2024-12-18 10:14:31

是的,你说得对。这里的“Count”表示滚动行数(Windows 中默认为 3 或 -3)

Yes, you're right. "Count" here means number of scroll lines (3 or -3 by default in Windows)

若有似无的小暗淡 2024-12-18 10:14:31

可以在画布上使用 MouseWheelListener;唯一的事情是事件仅在具有焦点的组件上触发。

画布通常不会获得焦点,除非您向其添加一个关键侦听器(即使是一个空侦听器)。

(我知道这个问题很老了,但面对同样的问题,谷歌引导我去那里,所以这里有一个解决方案)。

It is possible to use the MouseWheelListener on a canvas; the only thing is that the event trigger only on the component with the focus.

A canvas don't usually get focus, unless you add a key listener to it (even an empty one).

(I know the question is quite old, but faced with the same issue google directed me there so here's a solution).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文