【问题标题】:SWT: problems with clicking button after using setEnabled() on LinuxSWT:在 Linux 上使用 setEnabled() 后单击按钮出现问题
【发布时间】:2011-02-02 10:29:11
【问题描述】:

在使用setEnabled() 后,我对 SWT 和 Button 有一个奇怪的情况 - 似乎如果我至少禁用和启用按钮一次 - 我不能再用鼠标正确单击它了...已经将代码缩小到非常基本的:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TestButton {

    public TestButton() {
        Display display = new Display();
        Shell shell = new Shell(display);
        GridLayout mainLayout = new GridLayout();
        shell.setLayout(mainLayout);
        shell.setSize(100, 100);

        Button testButton = new Button(shell, SWT.PUSH);
        testButton.addSelectionListener(new TestClickListener());
        testButton.setText("Click me!");
        //testButton.setEnabled(false);
        //testButton.setEnabled(true);

        shell.open();

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

    class TestClickListener implements SelectionListener {

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }

        @Override
        public void widgetSelected(SelectionEvent e) {
            System.out.println("Click!");
        }
    }

    public static void main(String[] args) {
       new TestButton();
    }

}

当我将这两行注释掉时 - 我可以正确单击按钮并始终得到“单击!”已记录,但如果我取消注释它们 - 然后我不能再用鼠标正确单击按钮 - 视觉上似乎单击了按钮,但没有记录任何内容......

我在这里做错了吗?或者可能是 Linux 平台上的某种错误?因为在 Mac 上运行相同的代码我从来没有遇到过这样的问题......

感谢任何提示!

附:在 Ubuntu 9.10、Gnome + Compiz、Sun Java 1.6.0.16 上运行代码

【问题讨论】:

    标签: java events button swt click


    【解决方案1】:

    或者可能是 Linux 平台上的某种错误?

    在 Linux 上使用最新版本的 eclipse,它使用 SWT,一些对话框有按钮,单击它什么都不做。也许你遇到了同样的事情。这可以通过在启动 eclipse 时在环境变量中指定 GDK_NATIVE_WINDOWS=1 来解决。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-30
    • 2019-05-28
    • 2011-03-09
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    相关资源
    最近更新 更多