Java开发GUI之列表
awt包中的List控件可以创建一个选择列表,此列表可以支持单选,也可以支持多选。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| static void ListTest(){ Frame frame = new Frame("List"); Panel pannel = new Panel(); List list = new List(); list.add("鸣人"); list.add("佐助"); list.add("卡卡西"); list.add("小樱"); list.add("釉"); list.add("大蛇丸"); list.setMultipleSelections(true); list.addItemListener(new ListListener()); list.addActionListener(new ListListener()); pannel.add(list); frame.add(pannel); frame.pack(); frame.show(); }
|
ListListener类的简单实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class ListListener implements ActionListener,ItemListener{
@Override public void itemStateChanged(ItemEvent e) { System.out.println(e.getSource()); }
@Override public void actionPerformed(ActionEvent e) { System.out.println(e); System.out.println("======"); } }
|
List控件中的方法解析:
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
| public List();
public List(int rows);
public List(int rows, boolean multipleMode);
public int getItemCount();
public int countItems();
public String getItem(int index);
public synchronized String[] getItems();
public void add(String item);
public void addItem(String item);
public void add(String item, int index);
public synchronized void addItem(String item, int index);
public synchronized void replaceItem(String newValue, int index);
public void removeAll();
public synchronized void clear();
public synchronized void remove(String item);
public void remove(int position);
public void delItem(int position);
public synchronized int getSelectedIndex();
public synchronized int[] getSelectedIndexes();
public synchronized String getSelectedItem();
public synchronized String[] getSelectedItems();
public Object[] getSelectedObjects();
public void select(int index);
public synchronized void deselect(int index);
public boolean isIndexSelected(int index);
public boolean isSelected(int index);
public int getRows();
public boolean isMultipleMode();
public boolean allowsMultipleSelections();
public void setMultipleMode(boolean b);
public synchronized void setMultipleSelections(boolean b);
public synchronized void addItemListener(ItemListener l);
public synchronized void removeItemListener(ItemListener l);
public synchronized ItemListener[] getItemListeners();
public synchronized void addActionListener(ActionListener l);
public synchronized void removeActionListener(ActionListener l);
public synchronized ActionListener[] getActionListeners();
|