简体   繁体   English

SWT中的鼠标事件

[英]Mouse events in SWT

I defined a Node class extends Canvas class and handle the mouse event. 我定义了一个Node类扩展Canvas类并处理鼠标事件。

public class Node extends Canvas {
String name;

public String getName() { return name; }

public Node(Composite parent, int style, String name) {
    super(parent, style);
    // TODO Auto-generated constructor stub

    this.name = name;       
    this.setBackground(new Color(Display.getCurrent(), 0, 0, 0));

    this.addMouseListener(new MouseListener() {

        @Override
        public void mouseUp(MouseEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println("Mouse up (" + arg0.x + ", " + arg0.y + ") at node " + getName());
        }

        @Override
        public void mouseDown(MouseEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println("Mouse down (" + arg0.x + ", " + arg0.y + ") at node " + getName());
        }

        @Override
        public void mouseDoubleClick(MouseEvent arg0) {
            System.out.println("Double click at node " + getName());

        }
    });
}

and then I created a Composite object and add two Node objects: 然后我创建了一个Composite对象并添加了两个Node对象:

    Node node1 = new Node(this, SWT.NONE, "node 1");
    node1.setBounds(25, 25, 50, 50);

    Node node2 = new Node(this, SWT.NONE, "node 2");
    node2.setBounds(35, 35, 60, 60);
    node2.setBackground(new Color(Display.getCurrent(), 75, 75, 75));

Note that I chose the position of nodes such that they share some common areas. 请注意,我选择了节点的位置,以便它们共享一些公共区域。 Using color to differentiate between two nodes, I recognized that node1 is shown at the top, while node2 is shown behind. 使用颜色区分两个节点,我发现node1显示在顶部,而node2显示在后面。 If I apply mouse events in the sharing areas, node1 handle the events and node2 doesn't. 如果我在共享区域中应用鼠标事件,则node1处理事件而node2不处理。

node2 is added to the composite after node1 , so I expected node2 will have the privilege, ie if I apply mouse events to sharing areas, node2 should handle the events. node2被添加到node1之后的复合中,所以我希望node2具有该权限,即如果我将鼠标事件应用于共享区域,则node2应该处理事件。

How to fix this problem? 如何解决这个问题?

As you can see that the control that is visible (and above the other) gets the mouse events. 正如您所看到的那样,可见(并且在另一个之上)的控件会获得鼠标事件。 I think this behavior is correct and expected. 我认为这种行为是正确和预期的。 I don't suppose you are asking that even though node1 is above node2 and drawn above but still lets node2 receive all the mouse events. 我不认为你问的是,即使node1node2之上并且在上面绘制,但仍然让node2接收所有鼠标事件。 Which might not be possible using standard ways. 使用标准方法可能无法实现。

If however you are asking about ensuring that node2 appears above node1 and thus receives mouse events, you can reverse the creation order, or you can use Control#moveAbove(Control) like below: 但是,如果您要求确保node2出现在node1上方并因此接收鼠标事件,则可以反转创建顺序,或者您可以使用Control#moveAbove(Control)如下所示:

Node node1 = new Node(this, SWT.NONE, "node 1");
node1.setBounds(25, 25, 50, 50);

Node node2 = new Node(this, SWT.NONE, "node 2");
node2.setBounds(35, 35, 60, 60);
node2.setBackground(new Color(Display.getCurrent(), 75, 75, 75));

// Use moveAbove(null) to move node2 above all its siblings
node2.moveAbove(node1);

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

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