列表组件的应用举例
墨初 知识笔记 41阅读
When doing homework, I didn't do a lot of homework about list components because of lack of time. He carefully read this example in the book, output and run 1 to import Java. awt。 *; 2 import Java. awt。 Events. *; 3 public classtasklistendsframements actionlistener 4 {5 privatebuttonadd=newbutton (style=' color:rgba (0,0,1)'' add''); 6privateButtondel=newButton ('delete'); 7privateButtonup=newButton ('increase priority'); 8privateButtondown=newButton ('reduce priority'); 9 private 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

标签: