该例子演示了SWT界面下的下拉框Combo构建及监听。
import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class ComboExample { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Combo Example"); shell.setBounds(100, 100, 200, 100); shell.setLayout(new FillLayout(SWT.VERTICAL)); final Combo combo1 = new Combo(shell, SWT.READ_ONLY); final Combo combo2 = new Combo(shell, SWT.DROP_DOWN); final Label label = new Label(shell, SWT.CENTER); combo1.setItems(new String[] { "First", "Second", "Third" }); combo1.setText("First"); combo1.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent event) { label.setText("Selected: " + combo1.getText()); } }); combo2.setItems(new String[] { "First", "Second", "Third" }); combo2.setText("First"); combo2.addModifyListener(new ModifyListener() { public void modifyText(ModifyEvent event) { label.setText("Entered: " + combo2.getText()); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } }运行效果:
本文为风林火山博客原创,转载请注明出处:www.flcoder.com
谢谢,你的文章让我受到了启发。
该评论同步自 B3log 社区