swt combo监听
时间: 2024-01-05 18:21:01 浏览: 136
以下是使用SWT Combo监听事件的示例代码:
```java
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ComboListenerExample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
Combo combo = new Combo(shell, SWT.NONE);
combo.setItems(new String[] {"Option 1", "Option 2", "Option 3"});
combo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Combo selectedCombo = (Combo) e.widget;
String selectedOption = selectedCombo.getText();
System.out.println("Selected option: " + selectedOption);
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
```
这个示例创建了一个SWT Combo对象,并添加了几个选项。然后,通过添加SelectionListener来监听Combo的选择事件。当用户选择一个选项时,会触发widgetSelected方法,并打印所选选项的文本。
阅读全文