package swt; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Shell; public class ListDemo_03 { public static void main(String[] args) { //负责和操作系统交互,如读取底层事件等 Display display= new Display(); //窗口 Shell topShell= new Shell(display); topShell.setText( "测试list控件" ); topShell.setSize( 800 , 500 ); //不设置布局不显示,设置布局和设置bounds冲突 // topShell.setLayout(new GridLayout()); String[] items={ "a" , "a1" , "a2" , "a3" , "a4" , "a5" , "a6" }; String[] items2= new String[ 0 ]; // List list=new List(topShell, SWT.SINGLE|SWT.VERTICAL); // list.setItems(items); // list.setSelection(2); final List list2= new List(topShell, SWT.MULTI|SWT.VERTICAL); list2.setBounds( 0 , 0 , 100 , 280 ); list2.setItems(items); // list2.setSelection(2,4); final List list3= new List(topShell, SWT.MULTI|SWT.VERTICAL); list3.setBounds( 150 , 0 , 100 , 280 ); list3.setItems(items2); SelectionAdapter selectionAdapter= new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { Button source = (Button) e.getSource(); String text = source.getText(); if (text.equals( ">" )) { String[] selections = list2.getSelection(); for ( int i = 0 ; i < selections.length; i++) { list2.remove(selections[i]); list3.add(selections[i]); } } else if ( ">>" .equals(text)){ } else if ( "<" .equals(text)){ } else if ( "<<" .equals(text)){ } else if ( "上" .equals(text)){ int selectionIndex = list3.getSelectionIndex(); String currentValue = list3.getItem(selectionIndex); list3.setItem(selectionIndex, list3.getItem(selectionIndex- 1 )); list3.setItem(selectionIndex- 1 , currentValue); } else if ( "下" .equals(text)){ } } }; Button button= new Button(topShell, SWT.LEFT); button.setText( ">" ); button.addSelectionListener(selectionAdapter); button.setBounds( 110 , 0 , 30 , 20 ); Button button2= new Button(topShell, SWT.LEFT); button2.setText( "<" ); Button button3= new Button(topShell, SWT.LEFT); button3.setText( ">>" ); Button button4= new Button(topShell, SWT.LEFT); button4.setText( "<<" ); Button button5= new Button(topShell, SWT.LEFT); button5.setText( "上" ); button5.addSelectionListener(selectionAdapter); button5.setBounds( 110 , 50 , 30 , 20 ); Button button6= new Button(topShell, SWT.LEFT); button6.setText( "下" ); // topShell.pack(); topShell.open(); while (!topShell.isDisposed()){ if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } } |