Java知多少(88)列表和组合框

列表和组合框是又一类供用户选择的界面组件,用于在一组选择项目选择,组合框还可以输入新的选择。

列表

列表(JList)在界面中表现为列表框,是JList类或它的子类的对象。程序可以在列表框中加入多个文本选择项条目。列表事件的事件源有两种:

  • 一是鼠标双击某个选项:双击选项是动作事件,与该事件相关的接口是ActionListener,注册监视器的方法是addActionListener(),接口方法是actionPerformed(ActionEvent e)。
  • 二是鼠标单击某个选项:单击选项是选项事件,与选项事件相关的接口是ListSelectionListener,注册监视器的方法是addListSelectionListener,接口方法是valueChanged(ListSelectionEvent e)。


JList 类的常用构造方法:

  1. JList():建立一个列表。
  2. JList(String list[]):建立列表,list是字符串数组,数组元素是列表的选择条目。


JList类的常用方法:

  1. getSelectedIndex():获取选项的索引。返回最小的选择单元索引;只选择了列表中单个项时,返回该选择。
  2. getSelectedValue():获取选项的值。
  3. getSelectedIndices():返回所选的全部索引的数组(按升序排列)。
  4. getSelectedValues(),:返回所有选择值的数组,根据其列表中的索引顺序按升序排序。
  5. getItemCount():获取列表中的条数。
  6. setVisibleRowCount(int n):设置列表可见行数。
  7. setSelectionMode(int seleMode):设置列表选择模型。选择模型有单选和多选两种。
    • 单选:ListSelectionModel.SINGLE_SELECTION.
    • 多选:ListSelectionModel.MULTIPLE.INTERVAL_SELECTION.
  8. remove(int n):从列表的选项菜单中删除指定索引的选项。
  9. removeAll():删除列表中的全部选项。


列表可以添加滚动条,列表添加滚动条的方法是先创建列表,然后再创建一个JScrollPane滚动面板对象,在创建滚动面板对象时指定列表。以下代码示意为列表list2添加滚动条:
    JScrollPane jsp = new JScrollPane(list2);

【例 11-13】小应用程序有两个列表,第一个列表只允许单选,第二个列表允许多选。

 1 import java.applet.*;
 2 import javax.swing.*;
 3 import java.awt.*;
 4 import java.awt.event.*;
 5 class MyWindow extends JFrame implements ListSelectionListener{
 6     JList list1,list2;
 7     String news[]={"人民日报","新民晚报","浙江日报","文汇报"};
 8     String sports[]={"足球","排球","乒乓球","篮球"};
 9     JTextArea text;
10     MyWindow(String s){
11         super(s);
12         Container con = getContentPane();
13         con.setBackground(Color.BLUE);
14         con.setLayout(new GridLayout(2,2));
15         con.setSize(200,500);
16         list1 = new JList(news);
17         list1.setVisibleRowCount(3);
18         list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
19         list1.addListSelectionListener(this);
20         list2 = new JList(sports);
21         list2.setVisibleRowCount(2);
22         list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
23         list2.addListSelectionListener(this);
24         con.add(list1);
25         con.add(list2);
26         text= new JTextArea(10,20);
27         con.add(text);
28         this.setVisible(true);
29         this.pack();
30     }
31     public void valueChanged(ListSelectionEvent e){// 每当选择值发生更改时调用
32         if(e.getSource()==list1){
33             text.setText(null);
34             Object listValue = ((JList) e.getSource()).getSelectedValue();
35             String seleName = listValue.toString();
36             for(int i=0;i<news.length;i++)
37                 if(news[i].equals(seleName)){
38                     text.append(seleName+ "被选中\n");
39                 }
40         }
41         else if(e.getSource()==list2){
42             text.setText(null);
43             int tempList[] =list2.getSelectedIndices();
44             for(int i=0;i<tempList.length;i++)
45                 text.append(sports[tempList[i]] + "被选中\n");
46          }
47      }
48  }
49 public class Example6_3 extends Applet{
50     MyWindow myWin = new MyWindow("列表示例");
51 }

组合框

组合框(JComboBox)是文本框和列表的组合,可以在文本框中输入选项,也可以单击下拉按钮从显示的列表中进行选择。

组合框的常用构造方法:

  1. JComboBox():建立一个没有选项的JComboBox对象。
  2. JComboBox(JComboBoxModel aModel):用数据模型建立一个JComboBox对象。
  3. JComboBox(Object[]items):利用数组对象建立一个JComboBox对象。


组合框的其他常用方法有以下几个:

  1. addItem(Object obj):向组合框加选项。
  2. getItemCount():获取组合框的条目总数。
  3. removeItem(Object ob):删除指定选项。
  4. removeItemAt(int index):删除指定索引的选项。
  5. insertItemAt(Object ob,int index):在指定的索引处插入选项。
  6. getSelectedIndex():获取所选项的索引值(从0开始)。
  7. getSelectedItem():获得所选项的内容。
  8. setEditable(boolean b):设为可编辑。组合框的默认状态是不可编辑的,需要调用本方法设定为可编辑,才能响应选择输入事件。


在JComboBox对象上发生事件分为两类。一是用户选定项目,事件响应程序获取用户所选的项目。二是用户输入项目后按回车键,事件响应程序读取用户的输入。第一类事件的接口是ItemListener;第二类事件是输入事件,接口是ActionListener。

【例 11-14】一个说明组合框用法的应用程序。程序中声明的组合框子类实现ItemLister接口和ActionListener接口。组合框子类的窗口中设置了一个文本框和一个组合框,组合框中有三个选择。实现接口的监视方法将组合框的选择结果在文本框中显示。

 1 public class Example6_4{
 2     public static void main(String args[]){
 3         ComboBoxDemo mycomboBoxGUI = new ComboBoxDemo();
 4     }
 5 }
 6 class ComboBoxDemo extends JFrame implements ActionListener,ItemListener{
 7     public static final int Width = 350;
 8     public static final int Height = 150;
 9     String proList[] = { "踢足球","打篮球","打排球" };
10     JTextField text;
11     JComboBox comboBox;
12     public ComboBoxDemo(){
13         setSize(Width,Height);
14         setTitle("组合框使用示意程序");
15         Container conPane = getContentPane();
16         conPane.setBackground(Color.BLUE);
17         conPane.setLayout(new FlowLayout());
18         comboBox = new JComboBox(proList);
19         comboBox.addActionListener(this);
20         combobox.addItemListener(this);
21         comboBox.setEditable(true);//响应键盘输入
22         conPane.add(comboBox);
23         text = new JTextField(10);
24         conPane.add(text);
25         this.setVisible(true);
26     }
27     public void actionPerformed(ActionEvent e){
28         if(e.getSource()==comboBox)
29             text.setText(comboBox.getSelectedItem().toString());
30     }
31     public void itemStateChanged(ItemEvent e){
32         if(e.getSource()==comboBox){
33             text.setText(comboBox.getSelectedItem().toString());
34         }
35     }
36 }

系列文章:

posted @ 2015-06-10 19:33  Coda  阅读(2036)  评论(0编辑  收藏  举报