2

Having a Label and a Text control in SWT, how can I define a mnemonic on the label that will activate the input field? I found a couple of examples on how to set a mnemonic on a Button, but how do I define a mnemonic on a Label and make it point to a different input control?

4

1 回答 1

3

在最简单的情况下,您可以像使用按钮一样定义助记符。

Label label = new Label( parent, SWT.NONE );
label.setText( "&Name" );
Text text = new Text( parent, SWT.BORDER );

当按下Alt+N时,标签顺序中的下一个控件将被聚焦,在这种情况下,文本输入字段 。

如果另一个控件应该获得焦点,您需要将遍历侦听器添加到标签并手动将焦点赋予所需的控件。例如

Label label = new Label( parent, SWT.NONE );
label.setText( "&Name" );
label.addListener( SWT.Traverse, new Listener() {
  @Override
  public void handleEvent( Event event ) {
    if( event.detail == SWT.TRAVERSE_MNEMONIC ) {
      event.doit = false;
      otherControl.setFocus();
    }
  }
} );
于 2016-05-11T15:51:13.313 回答