【发布时间】:2016-09-21 09:27:43
【问题描述】:
我有 sn-p 课程。如何通过鼠标点击事件打破以下循环?
public class Sample {
private static Label label;
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setBounds(100, 100, 200, 70);
Button button = new Button(shell, SWT.NONE);
button.setText("Go");
button.addListener(SWT.Selection, new Listener(){
@Override
public void handleEvent(Event arg0) {
while (true) {
int x = MouseInfo.getPointerInfo().getLocation().x;
int y = MouseInfo.getPointerInfo().getLocation().y;
label.setText(x + ":" + y);
}
}});
label = new Label(shell, SWT.NONE);
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
【问题讨论】:
-
您首先要达到的目标是什么?
-
我想单击位于外部应用程序上的指定文本框,然后应该停止循环。我想记住点击的文本框位置 (x, y)。
-
如何知道外部应用程序中的文本框何时被点击?
-
我不需要知道何时单击了文本框。我需要知道是否点击了鼠标按钮,无论它是在哪里完成的。
-
所以您只想知道您的
button何时被点击?这就是SWT.SelectionListener已经在做的事情,那么为什么要使用while (true)循环呢?
标签: java loops swt mouse break