首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FocusListener在Mac上不起作用

FocusListener在Mac上不起作用
EN

Stack Overflow用户
提问于 2014-09-08 10:15:26
回答 1查看 308关注 0票数 1

我有以下代码:

代码语言:javascript
复制
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(3, true));
        shell.setText("One Potato, Two Potato");

        // Create the focus listener
        FocusListener listener = new FocusListener() {
            public void focusGained(FocusEvent event) {
                Button button = (Button) event.getSource();
                button.setText("I'm It!");
            }

            public void focusLost(FocusEvent event) {
                Button button = (Button) event.getSource();
                button.setText("Pick Me!");
            }
        };
        // Create the buttons and add the listener to each one
        for (int i = 0; i < 6; i++) {
            Button button = new Button(shell, SWT.PUSH);
            button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
            button.setText("Pick Me!");
            button.addFocusListener(listener);
        }
        // Display the window
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

但它似乎不起作用。我在Mac机器上开发Eclipse IDE。我做的事有什么问题吗?

EN

回答 1

Stack Overflow用户

发布于 2014-09-16 20:32:07

这可能不是你想要的答案,但我也注意到一些监听器在mac上不能工作( focus监听器在shell上也不能工作,其他的监听器在mac上也有but )。你最好的办法就是尝试用文本替换Button。让它看起来很像。即使在mac上,focus listener也可以处理文本。因此,您的代码将如下所示:

代码语言:javascript
复制
 Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(3, true));
    shell.setText("One Potato, Two Potato");

    // Create the focus listener
    FocusListener listener = new FocusListener() {
        public void focusGained(FocusEvent event) {
            Text text = (Text) event.getSource();
            text.setText("I'm It!");
        }

        public void focusLost(FocusEvent event) {
            Text text = (Text) event.getSource();
            text.setText("Pick Me!");
        }
    };
    // Create the buttons and add the listener to each one
    for (int i = 0; i < 6; i++) {
        Text text = new Text(shell, SWT.PUSH);
        text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        text.setText("Pick Me!");
        text.addFocusListener(listener);
    }
    // Display the window
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }

现在,您唯一需要做的就是更改字段,使其看起来更像一个按钮。希望能有所帮助。祝你好运!

附言:应该有人打开一个关于这个问题的bug。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25716610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档