列表和組合框是又一類供用戶選擇的界面組件,用于在一組選擇項目選擇,組合框還可以輸入新的選擇。
列表
列表(JList)在界面中表現為列表框,是JList類或它的子類的對象。程序可以在列表框中加入多個文本選擇項條目。列表事件的事件源有兩種:
一是鼠標雙擊某個選項:雙擊選項是動作事件,與該事件相關的接口是ActionListener,注冊監視器的方法是addActionListener(),接口方法是actionPerformed(ActionEvent e)。
二是鼠標單擊某個選項:單擊選項是選項事件,與選項事件相關的接口是ListSelectionListener,注冊監視器的方法是addListSelectionListener,接口方法是valueChanged(ListSelectionEvent e)。
JList 類的常用構造方法:
JList類的常用方法:
列表可以添加滾動條,列表添加滾動條的方法是先創建列表,然后再創建一個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對象上發生事件分為兩類。一是用戶選定項目,事件響應程序獲取用戶所選的項目。二是用戶輸入項目后按回車鍵,事件響應程序讀取用戶的輸入。第一類事件的接口是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()); } }}
新聞熱點
疑難解答