简体   繁体   English

在SWT中的mouseClick上触发事件?

[英]Fire an event on mouseClick in SWT?

In SWT, for MouseListener interface, the methods available are mouseUp() , mouseDown() and mouseDoubleClick() 在SWT中,对于MouseListener接口,可用的方法是mouseUp()mouseDown()mouseDoubleClick()

How to fire an event based on the user click? 如何根据用户点击触发事件?

We can do this by conjunction of mouseUp() and mouseDown() but isn't there any trivial solution like mouseClick() method in SWT? 我们可以通过mouseUp()mouseDown()的结合来做到这一点,但是SWT中没有像mouseClick()方法这样的简单解决方案吗?

Thanks. 谢谢。

How would a mouse-click event be defined? 如何定义鼠标单击事件? Mouse-down followed by mouse-up without mouse leaving the bounds of the control (otherwise it would be drag-start), right? 鼠标下移然后鼠标上移,而鼠标不会离开控件的边界(否则将开始拖动),对吗? By that definition, mouse-click event couldn't be associated with a single point, but rather with an area (1) or a control (2). 根据该定义,鼠标单击事件不能与单个点关联,而可以与区域(1)或控件(2)关联。 The first case wouldn't fit into generic SWT event, which only has a location (x and y), and you'd still need additional code to check whether the click area was inside your image. 第一种情况不适用于仅具有位置(x和y)的通用SWT事件,并且您仍然需要其他代码来检查单击区域是否在图像内。 In the second case where the mouse-click would only be defined using a control (and no location), the event would be useless to you. 在第二种情况下,仅使用控件(而不是位置)来定义鼠标单击,该事件对您没有用。

When you have implemented your own single-click detection you can fire any events on the control you like, even those not defined by SWT. 实现自己的单击检测后,您可以在喜欢的控件上触发任何事件,即使不是SWT定义的事件。

To react on a mouse click event on a button you can use SelectionListener, something like this should do the trick: 要对按钮上的鼠标单击事件做出反应,您可以使用SelectionListener,如下所示就可以解决问题:

button.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            super.widgetSelected(e);
            System.out.println("click!");
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM