代码之家  ›  专栏  ›  技术社区  ›  Laimoncijus

SWT:在Linux上使用setEnabled()后单击按钮的问题

  •  1
  • Laimoncijus  · 技术社区  · 16 年前

    setEnabled()

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

    当我把这两行注释掉-我可以正确地点击一个按钮,总是得到“点击记录,但如果我取消注释他们-然后我不能用鼠标正确点击按钮-按钮似乎被点击,但没有任何记录。。。

    谢谢你的提示!

    在Ubuntu 9.10,Gnome+Compiz,Sun Java 1.6.0.16上运行代码

    1 回复  |  直到 16 年前
        1
  •  1
  •   penpen    16 年前

    使用最新版本的eclipse,它使用SWT,在Linux上,一些对话框有一个按钮,点击它什么也不做。也许你遇到了同样的事情。这是通过指定 GDK_NATIVE_WINDOWS=1