我创建了自己的控件:
我想跟踪鼠标并添加了一个MouseTrackListener
. 不幸的是,当鼠标移到我的合成部分(即标签和按钮)上时,也会生成事件MouseEnter
。MouseLeave
[Mouse enter] - mouse enters the empty space
[Mouse hover] - mouse is over the empty space
[Mouse exit] - mouse moved over label
[Mouse enter] - mouse leaves label and enters empty space
[Mouse hover] - mouse over empty space
[Mouse exit] - mouse leaves composite
如何将复合材料作为一件完整的东西而不是子零件来跟踪?
public class MyComposite extends Composite {
public MyComposite(final Composite parent, final int style) {
super(parent, style);
final Label lbl = new Label(this, SWT.NONE);
lbl.setBounds(10, 10, 78, 15);
lbl.setText("My Composite");
final Button btn = new Button(this, SWT.NONE);
btn.setBounds(190, 29, 75, 25);
btn.setText("Ok");
pack();
}
public static void main(final String[] args) {
final Shell shell = new Shell(Display.getDefault());
shell.setText("Testcase");
shell.setLayout(new FillLayout());
final MyComposite comp = new MyComposite(shell, SWT.NONE);
comp.addMouseTrackListener(new MouseTrackListener() {
@Override
public void mouseHover(final MouseEvent e) {
System.out.println("[Mouse hover]");
}
@Override
public void mouseExit(final MouseEvent e) {
System.out.println("[Mouse exit]");
}
@Override
public void mouseEnter(final MouseEvent e) {
System.out.println("[Mouse enter]");
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}