Swing中提供兩種列表組件,分別是列表框(JList)和組合框(JComboBox)。
一、JList組件
構造方法:
public JList():構造一個空的、具有只讀模型的JList。
public JList(Object[] listData):構造一個顯示指定數組元素的JList。
public JList(Vector listData):構造一個 顯示指定Vector中元素的JList 。
public Jlist(ListModel dataModel):根據指定的非null模型構造一個JList。
常用方法:
public int getItemCount():獲取列表中的選項個數。
public void remove(int n):從列表的選項菜單中刪除指定索引的選項。
public void removeAll():刪除列表中的全部選項。
public int getSelectedIndex():返回最小的選擇單元索引;只選擇單個項時,返回該選擇單元索引。
public int[] getSelectedIndices():返回所選的全部索引的數組(按升序排列)。
public List<E> getSelectedValuesList():根據列表中的索引,按照增加的順序返回所有選定項目的列表。
public void setSelectionMode(int selectionMode):設置列表選擇模型(參數0、1、2對應單個、連續多個、不連續多個)。
事件:
列表事件有兩種:
一是鼠標雙擊某個選項:雙擊選項是動作事件,相關的接口是MouseListener,注冊監視器的方法是addMouseListener(),接口方法是mouseClicked(MouseEvent e)。
二是鼠標單擊某個選項:單擊選項是選項事件,相關的接口是ListSelectionListener,注冊監視器的方法是addListSelectionListener,接口方法是valueChanged(ListSelectionEvent e)。
代碼示例:
package com.java.swing; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Iterator; import java.util.Vector; import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class JListTest extends JFrame { /** * */ private static final long serialVersionUID = 1L; public JListTest() { Container cp = this.getContentPane(); this.setTitle("JList列表框"); this.setBounds(0, 0, 250, 170); this.setDefaultCloseOperation(EXIT_ON_CLOSE); cp.setLayout(null); // 方法一 String[] contents = { "1java", "1數據結構", "1數據庫", "1計算機系統", "1操作系統", "1C語言" }; JList<String> jl = new JList<>(contents); jl.setBounds(20, 10, 100, 100); jl.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); cp.add(jl); // // 方法二 // Vector<String> vector = new Vector<String>(); // JList<String> jl = new JList<>(vector); // vector.add("2java"); // vector.add("2數據結構"); // vector.add("2數據庫"); // vector.add("2計算機系統"); // vector.add("2操作系統"); // vector.add("2C語言"); // //方法三 // String[] contents = { "3java", "3數據結構", "3數據庫", "3計算機系統", "3操作系統", "3C語言" }; // DefaultListModel<String> model = new DefaultListModel<String>(); // for(String content:contents) // model.addElement(content); // JList<String> jl = new JList<String>(model); // jl.setBounds(20, 10, 100, 100); // cp.add(jl); JScrollPane js1 = new JScrollPane(jl); js1.setBounds(20, 10, 100, 100); cp.add(js1); JTextArea area = new JTextArea(); JScrollPane js2 = new JScrollPane(area); js2.setBounds(140, 10, 75, 100); cp.add(js2); // 注冊選項監視器 jl.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { // 獲取列表選中的元素 java.util.List<String> values = jl.getSelectedValuesList(); area.setText(""); Iterator<String> it = values.iterator(); while (it.hasNext()) { area.append(it.next() + "\n"); } } }); // 注冊動作事件監視器 jl.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub if (e.getClickCount() == 2) { java.util.List<String> values = jl.getSelectedValuesList(); area.setText(""); Iterator<String> it = values.iterator(); while (it.hasNext()) { area.append(it.next() + "\n"); } } } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } }); } public static void main(String args[]) { JListTest frame = new JListTest(); frame.setVisible(true); } }
二、JComboBox組件
構造方法:
public JComboBox():建立一個沒有選項的JComboBox對象。
public JComboBox(Object[] items):利用對象數組構造一個JComboBox對象。
public JComboBox(Vector<?> items):利用Vector構造一個JComboBox對象。
public JComboBox(ComboBoxModel<E> aModel) :創建一個 JComboBox ,從現有的 ComboBoxModel獲取其項目。
常用方法:
public void addItem(Object obj):向組合框加選項。
public int getItemCount():獲取組合框的條目總數。
public void removeItem(Object ob):刪除指定選項。
public void removeItemAt(int index):刪除指定索引的選項。
public void insertItemAt(Object ob,int index):在指定的索引處插入選項。
public int getSelectedIndex():獲取所選項的索引值(從0開始)。
public Object getSelectedItem():獲得所選項的內容。
public void setEditable(boolean b):設為可編輯。組合框的默認狀態是不可編輯的,需要調用本方法設定為可編輯,才能響應選擇輸入事件。
事件:
在JComboBox對象上發生事件分為兩類。
一是用戶選定項目,屬於選項事件,用於獲取用戶所選的項目,接口是ItemListener,注冊監視器的方法是addItemListener(),接口方法是itemStateChanged(ItemEvent e)。
二是用戶輸入項目后按回車鍵,屬於動作事件,用於讀取用戶的輸入,接口是ActionListener ,注冊監視器的方法是addActionListener (),接口方法是actionPerformed(ActionEvent e)。
代碼示例:
package com.java.swing; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.*; public class JComboBoxTest extends JFrame { /** * */ private static final long serialVersionUID = 1L; public JComboBoxTest() { Container cp = this.getContentPane(); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setTitle("下拉列表框"); this.setBounds(0, 0, 350, 200); cp.setLayout(null); JLabel myLable = new JLabel("請選擇課程:"); myLable.setBounds(20, 20, 80, 20); cp.add(myLable); // 方法一 JComboBox<String> comboBox = new JComboBox<String>(); comboBox.addItem("1java"); comboBox.addItem("1數據結構"); comboBox.addItem("1數據庫"); comboBox.addItem("1計算機系統"); comboBox.addItem("1操作系統"); comboBox.addItem("1C語言"); // // 方法二 // String[] contents = { "2java", "2數據結構", "2數據庫", "2計算機系統", "2操作系統", "2C語言"}; // JComboBox<String> comboBox = new JComboBox<String>(contents); // // 方法三 // String[] contents = { "3java", "3數據結構", "3數據庫", "3計算機系統", "3操作系統", "3C語言"}; // ComboBoxModel<String> model = new DefaultComboBoxModel<String>(contents); // JComboBox<String> comboBox = new JComboBox<String>(model); // comboBox.setModel(model); comboBox.setBounds(110, 20, 80, 20); comboBox.setEditable(true); // 設置為可編輯 cp.add(comboBox); JLabel lblResult = new JLabel(""); lblResult.setBounds(20, 57, 200, 15); cp.add(lblResult); // 注冊選項事件監視器 comboBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e){ lblResult.setText("選擇的課程:" + comboBox.getSelectedItem()); } }); // 注冊動作事件監視器 comboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { lblResult.setText("選擇的課程:" + comboBox.getSelectedItem()); } }); } public static void main(String[] args) { JComboBoxTest frame = new JComboBoxTest(); frame.setVisible(true); } }