有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java widgetDefaultSelected()方法从未触发?

假设我有两个单选按钮,我希望其中一个默认选中,并且我希望SelectionListener执行一些操作

当我尝试明显的方法时,它不起作用:

Button button = new Button(parent, SWT.RADIO) ;
button.setSelection(true) ;

button.addSelectionListener( new SelectionAdapter() {
    public void widgetDefaultSelected(SelectionEvent e){
        doAction() ;
    }
}) ;

doAction()从未被触发

有人能解释为什么默认选择的SelectionEvent从未被触发吗


共 (2) 个答案

  1. # 1 楼答案

    将代码更改为:

    button.addSelectionListener( new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e){
            doAction() ;
        }
    }) ;
    

    避免使用widgetDefaultSelected()

  2. # 2 楼答案

    For example, on some platforms default selection occurs in a List when the user double-clicks an item or types return in a Text. On some platforms, the event occurs when a mouse button or key is pressed. On others, it happens when the mouse or key is released. The exact key or mouse gesture that causes this event is platform specific.

    这个JavaDoc说明了一切。这是一种可能发生在某些Control上的平台依赖性操作。阿法伊克,带有SWT.CHECKButton不是其中之一