package swt; import org.eclipse.swt.SWT; import org.eclipse.swt.events.KeyAdapter; import org.eclipse.swt.events.KeyEvent; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** * 比list占用空间少 * @author zhao * */ public class ComboDemo_04 { public static void main(String[] args) { //负责和操作系统交互,如读取底层事件等 Display display= new Display(); //窗口 Shell topShell= new Shell(display,SWT.SHELL_TRIM|SWT.BORDER); topShell.setText( "测试Combo控件" ); topShell.setSize( 800 , 500 ); //不设置布局不显示 topShell.setLayout( new GridLayout()); String[] items={ "a" , "a1" , "a2" , "a3" , "a4" , "a5" , "a6" }; Combo combo= new Combo(topShell, SWT.DOWN); combo.setItems(items); final Combo combo2= new Combo(topShell, SWT.SIMPLE); combo2.setItems(items); combo2.addKeyListener( new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.keyCode==SWT.ARROW_DOWN) { combo2.select(combo2.getSelectionIndex()+ 1 ); } else if (e.keyCode==SWT.ARROW_UP) { combo2.select(combo2.getSelectionIndex()- 1 ); } } }); topShell.pack(); topShell.open(); while (!topShell.isDisposed()){ if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } } |