List组件运用的例子
  写作业的时候,因时间不够,没怎么写过有关List组件的作业
  
  将书上的这个例子仔细的看了看,输出来,运行了一下。
  1
import java.awt.*;
2
import java.awt.event.*;
3
public class  TaskList extends Frame implements ActionListener
4
{
5
    private Button add=new Button("增加");
6
    private Button del=new Button("删除");
7
    private Button up=new Button("增加优先级");
8
    private Button down=new Button("降低优先级");
9
    private List list=new List();
10
    private TextField taskInput=new TextField();
11
    private Label priorityLabel=new Label("改变优先级");
12
    private Label taskLabel=new Label("工作事项:");
13
    private class WindowCloser extends WindowAdapter
14
    {
15
        public void windowClosing(WindowEvent we)
16
        {
17
            System.exit(0);
18
        }
19
    }
20
    public TaskList()
21
    {
22
        super("工作事项表");
23
        setup();
24
        add.addActionListener(this);
25
        del.addActionListener(this);
26
        down.addActionListener(this);
27
        up.addActionListener(this);
28
        list.addActionListener(this);
29
        addWindowListener(new WindowCloser());
30
        pack();
31
        show();
32
    }
33
    private void setup()
34
    {
35
        Panel buttons=new Panel();
36
        buttons.setLayout(new FlowLayout());
37
        buttons.add(add);     
38
        buttons.add(del);
39
        Panel priorities=new Panel();
40
        priorities.setLayout(new FlowLayout());
41
        priorities.add(up);
42
        priorities.add(priorityLabel);
43
        priorities.add(down);
44
        Panel input=new Panel();
45
        input.setLayout(new BorderLayout());
46
        input.add("West",taskLabel);
47
        input.add("Center",taskInput);
48
        Panel top=new Panel();
49
        top.setLayout(new GridLayout(2,1));
50
        top.add(input);
51
        top.add(priorities);
52
        setLayout(new BorderLayout());
53
        add("Center",list);
54
        add("South",buttons);
55
        add("North",top);
56
    }
57
    public void actionPerformed(ActionEvent ae)
58
    {
59
        if((ae.getSource()==add)&&(!taskInput.getText().equals("")))
60
            handleAdd(taskInput.getText().trim());   //public String trim():删除该字符串两端的空格。
61
        if((ae.getSource()==del)&&(list.getSelectedIndex()>=0))  //getSelectedIndex():获取列表中选定项的下标
62
            handleDel(list.getSelectedIndex());
63
        if((ae.getSource()==up)&&(list.getSelectedIndex()>=0))
64
            handleIncPriority(list.getSelectedIndex());
65
        if((ae.getSource()==down)&&(list.getSelectedIndex()>=0))
66
            handleDecPriority(list.getSelectedIndex());
67
        if((ae.getSource()==list))
68
            taskInput.setText(list.getSelectedItem());
69
        taskInput.requestFocus();
70
    }
71
    private void handleAdd(String newTask)
72
    {
73
        list.add(newTask);  //add(String item):在滚动列表的末尾添加指定项
74
        list.select(list.getItemCount()-1);//getItemCount()获取列表的项数;
75
        taskInput.setText("");//select(int index)选择滚动列表中指定下标的项
76
    }
77
    private void handleDel(int pos)
78
    {
79
        list.remove(pos);//remove(int position)从此列表中删除指定位置的某项
80
        list.select(pos);
81
    }
82
    private void handleIncPriority(int pos)
83
    {
84
        String item=list.getItem(pos);
85
        list.remove(pos);
86
        list.add(item,pos-1);
87
        list.select(pos-1);
88
    }
89
    private void handleDecPriority(int pos)
90
    {
91
        if(pos<list.getItemCount()-1)
92
        {
93
            String item=list.getItem(pos);
94
            list.remove(pos);
95
            list.add(item,pos+1);
96
            list.select(pos+1);
97
        }
98
    }
99
    public static void main(String[] args) 
100
    {
101
        TaskList t=new TaskList();
102
    }
103
}
104
import java.awt.*;2
import java.awt.event.*;3
public class  TaskList extends Frame implements ActionListener4
{5
    private Button add=new Button("增加");6
    private Button del=new Button("删除");7
    private Button up=new Button("增加优先级");8
    private Button down=new Button("降低优先级");9
    private List list=new List();10
    private TextField taskInput=new TextField();11
    private Label priorityLabel=new Label("改变优先级");12
    private Label taskLabel=new Label("工作事项:");13
    private class WindowCloser extends WindowAdapter14
    {15
        public void windowClosing(WindowEvent we)16
        {17
            System.exit(0);18
        }19
    }20
    public TaskList()21
    {22
        super("工作事项表");23
        setup();24
        add.addActionListener(this);25
        del.addActionListener(this);26
        down.addActionListener(this);27
        up.addActionListener(this);28
        list.addActionListener(this);29
        addWindowListener(new WindowCloser());30
        pack();31
        show();32
    }33
    private void setup()34
    {35
        Panel buttons=new Panel();36
        buttons.setLayout(new FlowLayout());37
        buttons.add(add);     38
        buttons.add(del);39
        Panel priorities=new Panel();40
        priorities.setLayout(new FlowLayout());41
        priorities.add(up);42
        priorities.add(priorityLabel);43
        priorities.add(down);44
        Panel input=new Panel();45
        input.setLayout(new BorderLayout());46
        input.add("West",taskLabel);47
        input.add("Center",taskInput);48
        Panel top=new Panel();49
        top.setLayout(new GridLayout(2,1));50
        top.add(input);51
        top.add(priorities);52
        setLayout(new BorderLayout());53
        add("Center",list);54
        add("South",buttons);55
        add("North",top);56
    }57
    public void actionPerformed(ActionEvent ae)58
    {59
        if((ae.getSource()==add)&&(!taskInput.getText().equals("")))60
            handleAdd(taskInput.getText().trim());   //public String trim():删除该字符串两端的空格。61
        if((ae.getSource()==del)&&(list.getSelectedIndex()>=0))  //getSelectedIndex():获取列表中选定项的下标62
            handleDel(list.getSelectedIndex());63
        if((ae.getSource()==up)&&(list.getSelectedIndex()>=0))64
            handleIncPriority(list.getSelectedIndex());65
        if((ae.getSource()==down)&&(list.getSelectedIndex()>=0))66
            handleDecPriority(list.getSelectedIndex());67
        if((ae.getSource()==list))68
            taskInput.setText(list.getSelectedItem());69
        taskInput.requestFocus();70
    }71
    private void handleAdd(String newTask)72
    {73
        list.add(newTask);  //add(String item):在滚动列表的末尾添加指定项74
        list.select(list.getItemCount()-1);//getItemCount()获取列表的项数;75
        taskInput.setText("");//select(int index)选择滚动列表中指定下标的项76
    }77
    private void handleDel(int pos)78
    {79
        list.remove(pos);//remove(int position)从此列表中删除指定位置的某项80
        list.select(pos);81
    }82
    private void handleIncPriority(int pos)83
    {84
        String item=list.getItem(pos);85
        list.remove(pos);86
        list.add(item,pos-1);87
        list.select(pos-1);88
    }89
    private void handleDecPriority(int pos)90
    {91
        if(pos<list.getItemCount()-1)92
        {93
            String item=list.getItem(pos);94
            list.remove(pos);95
            list.add(item,pos+1);96
            list.select(pos+1);97
        }98
    }99
    public static void main(String[] args) 100
    {101
        TaskList t=new TaskList();102
    }103
}104

                    
                


    
                
            
        
浙公网安备 33010602011771号