使用 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 上运行代码
最佳答案
Or maybe it's some kind of bug on Linux platform?
在 Linux 上使用最新版本的 eclipse(使用 SWT)时,某些对话框有按钮,单击该按钮不会执行任何操作。也许你也遇到过同样的事情。这可以通过指定 GDK_NATIVE_WINDOWS=1
来解决。在环境变量中,启动 eclipse 时。
关于java - SWT:在 Linux 上使用 setEnabled() 后单击按钮出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2544734/