【问题标题】:How to break the loop on mouse click event如何打破鼠标点击事件的循环
【发布时间】: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.Selection Listener 已经在做的事情,那么为什么要使用 while (true) 循环呢?

标签: java loops swt mouse break


【解决方案1】:

我有我想要的。 感谢 Baz 的帮助。

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 ();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2022-01-25
    • 2018-06-29
    • 2014-12-13
    相关资源
    最近更新 更多