Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在鼠标点击事件中打破循环_Java_Loops_Swt_Mouse_Break - Fatal编程技术网

Java 如何在鼠标点击事件中打破循环

Java 如何在鼠标点击事件中打破循环,java,loops,swt,mouse,break,Java,Loops,Swt,Mouse,Break,我有snippet类。如何通过鼠标单击事件打破以下循环 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 Fi

我有snippet类。如何通过鼠标单击事件打破以下循环

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 ();
    }
}
我有我想要的。 谢谢巴兹的帮助

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) {
            Shell screenShell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
            screenShell.setMaximized(true);
            screenShell.setBackground(display.getSystemColor(SWT.COLOR_GRAY));
            screenShell.setAlpha(120);
            screenShell.addListener(SWT.MouseDown, new Listener() {
                @Override
                public void handleEvent(Event event) {
                    screenShell.close();                        
                }});
            screenShell.open();
            Display.getCurrent().timerExec(10, new Runnable(){
                @Override
                public void run() {
                    if (!screenShell.isDisposed()) {
                        int x = MouseInfo.getPointerInfo().getLocation().x;
                        int y = MouseInfo.getPointerInfo().getLocation().y;
                        label.setText(x + ":" + y);
                        Display.getCurrent().timerExec(10, this);
                    }
                }});    
        }});

    label = new Label(shell, SWT.NONE);
    shell.open();

    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}

您首先要实现的是什么?我想单击位于外部应用程序上的指定文本框,然后单击循环。我想记住单击的文本框位置x,y。如何知道外部应用程序中的文本框何时被单击?我不需要知道文本框何时被单击。我需要知道鼠标按钮是否被点击,无论它在哪里被点击。所以你只想知道你的按钮何时被点击?这就是SWT.Selection侦听器已经做的事情,那么为什么要使用while-true循环呢?