列表和組合框是又一類供用戶選擇的界面組件,用于在一組選擇項目選擇,組合框還可以輸入新的選擇。
列表
列表(JList)在界面中表現(xiàn)為列表框,是JList類或它的子類的對象。程序可以在列表框中加入多個文本選擇項條目。列表事件的事件源有兩種:
一是鼠標(biāo)雙擊某個選項:雙擊選項是動作事件,與該事件相關(guān)的接口是ActionListener,注冊監(jiān)視器的方法是addActionListener(),接口方法是actionPerformed(ActionEvent e)。
二是鼠標(biāo)單擊某個選項:單擊選項是選項事件,與選項事件相關(guān)的接口是ListSelectionListener,注冊監(jiān)視器的方法是addListSelectionListener,接口方法是valueChanged(ListSelectionEvent e)。
JList 類的常用構(gòu)造方法:
- JList():建立一個列表。
- JList(String list[]):建立列表,list是字符串?dāng)?shù)組,數(shù)組元素是列表的選擇條目。
JList類的常用方法:
- getSelectedIndex():獲取選項的索引。返回最小的選擇單元索引;只選擇了列表中單個項時,返回該選擇。
- getSelectedValue():獲取選項的值。
- getSelectedIndices():返回所選的全部索引的數(shù)組(按升序排列)。
- getSelectedValues(),:返回所有選擇值的數(shù)組,根據(jù)其列表中的索引順序按升序排序。
- getItemCount():獲取列表中的條數(shù)。
- setVisibleRowCount(int n):設(shè)置列表可見行數(shù)。
- setSelectionMode(int seleMode):設(shè)置列表選擇模型。選擇模型有單選和多選兩種。
- 單選:ListSelectionModel.SINGLE_SELECTION.
- 多選:ListSelectionModel.MULTIPLE.INTERVAL_SELECTION.
- remove(int n):從列表的選項菜單中刪除指定索引的選項。
- removeAll():刪除列表中的全部選項。
列表可以添加滾動條,列表添加滾動條的方法是先創(chuàng)建列表,然后再創(chuàng)建一個JScrollPane滾動面板對象,在創(chuàng)建滾動面板對象時指定列表。以下代碼示意為列表list2添加滾動條:
1
|
JScrollPane jsp = new JScrollPane(list2); |
【例】小應(yīng)用程序有兩個列表,第一個列表只允許單選,第二個列表允許多選。
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
|
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){ // 每當(dāng)選擇值發(fā)生更改時調(diào)用 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+ "被選中\(zhòng)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]] + "被選中\(zhòng)n" ); } } } public class Example6_3 extends Applet{ MyWindow myWin = new MyWindow( "列表示例" ); } |
組合框
組合框(JComboBox)是文本框和列表的組合,可以在文本框中輸入選項,也可以單擊下拉按鈕從顯示的列表中進(jìn)行選擇。
組合框的常用構(gòu)造方法:
- JComboBox():建立一個沒有選項的JComboBox對象。
- JComboBox(JComboBoxModel aModel):用數(shù)據(jù)模型建立一個JComboBox對象。
- JComboBox(Object[]items):利用數(shù)組對象建立一個JComboBox對象。
組合框的其他常用方法有以下幾個:
- addItem(Object obj):向組合框加選項。
- getItemCount():獲取組合框的條目總數(shù)。
- removeItem(Object ob):刪除指定選項。
- removeItemAt(int index):刪除指定索引的選項。
- insertItemAt(Object ob,int index):在指定的索引處插入選項。
- getSelectedIndex():獲取所選項的索引值(從0開始)。
- getSelectedItem():獲得所選項的內(nèi)容。
- setEditable(boolean b):設(shè)為可編輯。組合框的默認(rèn)狀態(tài)是不可編輯的,需要調(diào)用本方法設(shè)定為可編輯,才能響應(yīng)選擇輸入事件。
在JComboBox對象上發(fā)生事件分為兩類。一是用戶選定項目,事件響應(yīng)程序獲取用戶所選的項目。二是用戶輸入項目后按回車鍵,事件響應(yīng)程序讀取用戶的輸入。第一類事件的接口是ItemListener;第二類事件是輸入事件,接口是ActionListener。
【例】一個說明組合框用法的應(yīng)用程序。程序中聲明的組合框子類實現(xiàn)ItemLister接口和ActionListener接口。組合框子類的窗口中設(shè)置了一個文本框和一個組合框,組合框中有三個選擇。實現(xiàn)接口的監(jiān)視方法將組合框的選擇結(jié)果在文本框中顯示。
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
|
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 ); //響應(yīng)鍵盤輸入 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()); } } } |