简体   繁体   English

SWT按钮上的MouseClick事件在哪里?

[英]Where is the MouseClick event on an SWT Button?

I know this sounds like a very basic question, and I feel embarrassed to ask it but... 我知道这听起来像一个非常基本的问题,我觉得很尴尬但是...

How to I add a Mouse Click handler to an SWT button? 如何将鼠标单击处理程序添加到SWT按钮?

What I checked: 我检查了什么:

  • I can find tons of examples on how to add mouse down, mouse up or double click handlers (by assigning a MouseListener ). 我可以找到大量关于如何添加鼠标按下,鼠标按下或双击处理程序的示例(通过分配MouseListener )。 Obviously, mouse click (sequence of down and up on the same control) is something different than mouse down. 显然,鼠标单击(同一控件上向下和向上的顺序)与鼠标向下不同。
  • I understand that there might not be a click handler on generic controls, but the only event I see added on the Button control is a SelectionListener -- that might be it, but selection sounds more like "received focus" to me than "was clicked or selected and then invoked with a key press". 我理解在泛型控件上可能没有单击处理程序,但是我在Button控件上看到的唯一事件是一个SelectionListener - 可能就是它,但选择听起来更像是“收到焦点”给我而不是“被点击”或者选择,然后用按键调用“。
  • I found a related question , whose answer basically says that you need to implement that yourself -- I find that a bit hard to believe. 我发现了一个相关的问题 ,他的回答基本上说你需要自己实现 - 我觉得有点难以置信。

Is selection what is commonly known as "OnClick" in other languages/frameworks? 选择在其他语言/框架中通常称为“OnClick”的是什么? Or is there something else that I completely missed? 或者还有其他我完全错过的东西?

Yes, SWT.Selection or SelectionListener is what you're looking for: 是的, SWT.SelectionSelectionListener是您正在寻找的:

Button button = new Button(shell, SWT.PUSH);
button.addListener(SWT.Selection, new Listener()
{
    @Override
    public void handleEvent(Event event)
    {
        System.out.println("SWT.Selection");
    }
});

adding a SelectionListener internally does the same as the code above. 在内部添加SelectionListener与上面的代码相同。

It might be called selection, because a Button can be a checkbox or a radion button depending on it's style. 它可能被称为选择,因为Button可以是复选框或radion按钮,具体取决于它的样式。

Yes SelectionListener is what you are after. SelectionListener就是你想要的。 I, too, am more of a fan of the OnClick terminology myself as it is more cut and dry; 我自己也更喜欢OnClick术语,因为它更加切割和干燥; I digress. 我离题了。

Here is a good example for you to see how it works: http://www.java2s.com/Tutorial/Java/0280__SWT/UsingSelectionListener.htm 以下是一个很好的示例,您可以看到它是如何工作的: http//www.java2s.com/Tutorial/Java/0280__SWT/UsingSelectionListener.htm

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

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