在位置获取SWT小部件

时间:2012-11-08 06:51:39

标签: java swt coordinates composite

我有一组相对于复合材料的x / y坐标,如何找到那些coords落入的小部件?

1 个答案:

答案 0 :(得分:5)

这很难看,但你可以做这样的功能:

public Control getControl(Composite composite, int x, int y)
{
  Control control = null;
  Control[] children = composite.getChildren()
  if (children.length == 0)
     control = composite;
  else
    for (Control tmp : children) {
       // The check below will not work because x, y and the control's bounds could be
       // relative to different parents... Better to convert all coordinates to display
       // by using Control.toDisplay() and then compare below
       if (tmp.getBounds().contains(x, y)) 
       {
         if (control instanceof Composite)
             control = getControl(tmp, x, y);
         else
             control =  tmp;
         break;
       }

    }

  return control;
}

如果使用MouseListener获取坐标,则只需使用:

event.getSource();