org.eclipse.swt.widgets.List控件的使用

时间:2024-04-12 10:54:43

演示swt里面List列表控件的使用:

org.eclipse.swt.widgets.List控件的使用

实现的java代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

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(800500);

        //不设置布局不显示,设置布局和设置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(00100280);

        list2.setItems(items);

//      list2.setSelection(2,4);

        final List list3=new List(topShell, SWT.MULTI|SWT.VERTICAL);

        list3.setBounds(1500100280);

        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(11003020);

        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(110503020);

        Button button6=new Button(topShell, SWT.LEFT);

        button6.setText("下");

         

         

//      topShell.pack();

        topShell.open();

        while(!topShell.isDisposed()){

            if (!display.readAndDispatch()) {

                display.sleep();

            }

        }

        display.dispose();

    }

}