列表和組合框是又一類供使用者選擇的介面組件,用於在一組選擇項目選擇,組合框還可以輸入新的選擇。
列表
列表(JList)在介面中表現為列表框,是JList類或它的子類的對象。程式可以在列表框中加入多個文本選擇項條目。列表事件的事件來源有兩種:
一是滑鼠雙擊某個選項:雙擊選項是動作事件,與該事件相關的介面是ActionListener,註冊監視器的方法是addActionListener(),介面方法是actionPerformed(ActionEvent e)。
二是按一下滑鼠某個選項:單擊選項是選項事件,與選項事件相關的介面是ListSelectionListener,註冊監視器的方法是addListSelectionListener,介面方法是valueChanged(ListSelectionEvent e)。
JList 類的常用構造方法:
- JList():建立一個列表。
- JList(String list[]):建立列表,list是字串數組,數組元素是列表的選擇條目。
JList類的常用方法:
- getSelectedIndex():擷取選項的索引。返回最小的選擇單元索引;只選擇了列表中單個項時,返回該選擇。
- getSelectedValue():擷取選項的值。
- getSelectedIndices():返回所選的全部索引的數組(按升序排列)。
- getSelectedValues(),:返回所有選擇值的數組,根據其列表中的索引順序按升序排序。
- getItemCount():擷取列表中的條數。
- setVisibleRowCount(int n):設定列表可見行數。
- setSelectionMode(int seleMode):設定列表選擇模型。選擇模型有單選和多選兩種。
- 單選:ListSelectionModel.SINGLE_SELECTION.
- 多選:ListSelectionModel.MULTIPLE.INTERVAL_SELECTION.
- remove(int n):從列表的選項菜單中刪除指定索引的選項。
- removeAll():刪除列表中的全部選項。
列表可以添加捲軸,列表添加捲軸的方法是先建立列表,然後再建立一個JScrollPane滾動面板對象,在建立滾動面板對象時指定列表。以下代碼示意為列表list2添加捲軸:
JScrollPane jsp = new JScrollPane(list2);
【例】小應用程式有兩個列表,第一個列表只允許單選,第二個列表允許多選。
import java.applet.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;class MyWindow extends JFrame implements ListSelectionListener{ JList list1,list2; String news[]={"人民日報","新民晚報","浙江日報","文彙報"}; String sports[]={"足球","排球","乒乓球","籃球"}; JTextArea text; MyWindow(String s){ super(s); Container con = getContentPane(); con.setBackground(Color.BLUE); con.setLayout(new GridLayout(2,2)); con.setSize(200,500); list1 = new JList(news); list1.setVisibleRowCount(3); list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list1.addListSelectionListener(this); list2 = new JList(sports); list2.setVisibleRowCount(2); list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); list2.addListSelectionListener(this); con.add(list1); con.add(list2); text= new JTextArea(10,20); con.add(text); this.setVisible(true); this.pack(); } public void valueChanged(ListSelectionEvent e){// 每當選擇值發生更改時調用 if(e.getSource()==list1){ text.setText(null); Object listValue = ((JList) e.getSource()).getSelectedValue(); String seleName = listValue.toString(); for(int i=0;i<news.length;i++) if(news[i].equals(seleName)){ text.append(seleName+ "被選中\n"); } } else if(e.getSource()==list2){ text.setText(null); int tempList[] =list2.getSelectedIndices(); for(int i=0;i<tempList.length;i++) text.append(sports[tempList[i]] + "被選中\n"); } } }public class Example6_3 extends Applet{ MyWindow myWin = new MyWindow("列表示例");}
組合框
組合框(JComboBox)是文字框和列表的組合,可以在文字框中輸入選項,也可以單擊下拉按鈕從顯示的列表中進行選擇。
組合框的常用構造方法:
- JComboBox():建立一個沒有選項的JComboBox對象。
- JComboBox(JComboBoxModel aModel):用資料模型建立一個JComboBox對象。
- JComboBox(Object[]items):利用數組對象建立一個JComboBox對象。
組合框的其他常用方法有以下幾個:
- addItem(Object obj):向組合框加選項。
- getItemCount():擷取組合框的條目總數。
- removeItem(Object ob):刪除指定選項。
- removeItemAt(int index):刪除指定索引的選項。
- insertItemAt(Object ob,int index):在指定的索引處插入選項。
- getSelectedIndex():擷取所選項的索引值(從0開始)。
- getSelectedItem():獲得所選項的內容。
- setEditable(boolean b):設為可編輯。組合框的預設狀態是不可編輯的,需要調用本方法設定為可編輯,才能響應選擇輸入事件。
在JComboBox對象上發生事件分為兩類。一是使用者選定項目,事件響應程式擷取使用者所選的項目。二是使用者輸入項目後按斷行符號鍵,事件響應程式讀取使用者的輸入。第一類事件的介面是ItemListener;第二類事件是輸入事件,介面是ActionListener。
【例】一個說明組合框用法的應用程式。程式中聲明的組合框子類實現ItemLister介面和ActionListener介面。組合框子類的視窗中設定了一個文字框和一個組合框,組合框中有三個選擇。實現介面的監視方法將組合框的選擇結果在文字框中顯示。
public class Example6_4{ public static void main(String args[]){ ComboBoxDemo mycomboBoxGUI = new ComboBoxDemo(); }}class ComboBoxDemo extends JFrame implements ActionListener,ItemListener{ public static final int Width = 350; public static final int Height = 150; String proList[] = { "踢足球","打籃球","打排球" }; JTextField text; JComboBox comboBox; public ComboBoxDemo(){ setSize(Width,Height); setTitle("組合框使用示意程式"); Container conPane = getContentPane(); conPane.setBackground(Color.BLUE); conPane.setLayout(new FlowLayout()); comboBox = new JComboBox(proList); comboBox.addActionListener(this); combobox.addItemListener(this); comboBox.setEditable(true);//響應鍵盤輸入 conPane.add(comboBox); text = new JTextField(10); conPane.add(text); this.setVisible(true); } public void actionPerformed(ActionEvent e){ if(e.getSource()==comboBox) text.setText(comboBox.getSelectedItem().toString()); } public void itemStateChanged(ItemEvent e){ if(e.getSource()==comboBox){ text.setText(comboBox.getSelectedItem().toString()); } }}