import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* <p>Title: 列表框</p>
* <p>Description: 通過輸入框添加元素和點擊“刪除”按鈕刪除列表元素</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: ListDemo.java</p>
* @version 1.0
*/
public class ListDemo extends JPanel
implements ListSelectionListener {
private JList list;
private DefaultListModel listModel;
private static final String hireString = "添加";
private static final String fireString = "刪除";
private JButton fireButton;
private JTextField employeeName;
public ListDemo() {
super(new BorderLayout());
//構建List的列表元素
listModel = new DefaultListModel();
listModel.addElement("Alan Sommerer");
listModel.addElement("Alison Huml");
listModel.addElement("Kathy Walrath");
listModel.addElement("Lisa Friendly");
listModel.addElement("Mary Campione");
listModel.addElement("Sharon Zakhour");
//創建一個List構件,並將列表元素添加到列表中
list = new JList(listModel);
//設置選擇模式為單選
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//初始化選擇索引在0的位置,即第一個元素
list.setSelectedIndex(0);
list.addListSelectionListener(this);
//設置列表可以同時看5個元素
list.setVisibleRowCount(5);
//給列表添加一個滑動塊
JScrollPane listScrollPane = new JScrollPane(list);
JButton hireButton = new JButton(hireString);
HireListener hireListener = new HireListener(hireButton);
hireButton.setActionCommand(hireString);
hireButton.addActionListener(hireListener);
hireButton.setEnabled(false);
fireButton = new JButton(fireString);
fireButton.setActionCommand(fireString);
fireButton.addActionListener(new FireListener());
employeeName = new JTextField(10);
employeeName.addActionListener(hireListener);
employeeName.getDocument().addDocumentListener(hireListener);
String name = listModel.getElementAt(
list.getSelectedIndex()).toString();
//創建一個面板
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new BoxLayout(buttonPane,
BoxLayout.LINE_AXIS));
buttonPane.add(fireButton);
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(new JSeparator(SwingConstants.VERTICAL));
buttonPane.add(Box.createHorizontalStrut(5));
buttonPane.add(employeeName);
buttonPane.add(hireButton);
buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
add(listScrollPane, BorderLayout.CENTER);
add(buttonPane, BorderLayout.PAGE_END);
}
/**
*<br>類說明:“添加”按鈕監聽
*<br>類描述:當點擊“添加”按鈕後,實現將元素添加到列表框中
*/
class FireListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
int index = list.getSelectedIndex();
listModel.remove(index);
int size = listModel.getSize();
if (size == 0) { //如果沒有了選擇項,則是“刪除”按鈕實效
fireButton.setEnabled(false);
} else { //選擇了一個
if (index == listModel.getSize()) {
//移除選項
index--;
}
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
}
}
/**
*<br>類說明:“刪除”按鈕監聽事件
*<br>類描述:實現刪除列表元素
*/
class HireListener implements ActionListener, DocumentListener {
private boolean alreadyEnabled = false;
private JButton button;
public HireListener(JButton button) {
this.button = button;
}
//必須實現 ActionListener.
public void actionPerformed(ActionEvent e) {
String name = employeeName.getText();
//如果輸入空或有同名
if (name.equals("") || alreadyInList(name)) {
Toolkit.getDefaultToolkit().beep();
employeeName.requestFocusInWindow();
employeeName.selectAll();
return;
}
int index = list.getSelectedIndex(); //獲取選擇項
if (index == -1) { //如果沒有選擇,就插入到第一個
index = 0;
} else { //如果有選擇,那麼插入到選擇項的後面
index++;
}
listModel.insertElementAt(employeeName.getText(), index);
//重新設置文本
employeeName.requestFocusInWindow();
employeeName.setText("");
//選擇新的元素,並顯示出來
list.setSelectedIndex(index);
list.ensureIndexIsVisible(index);
}
/**
*<br>方法說明:檢測是否在LIST中有重名元素
*<br>輸入參數:String name 檢測的名字
*<br>返回類型:boolean 布爾值,如果存在返回true
*/
protected boolean alreadyInList(String name) {
return listModel.contains(name);
}
/**
*<br>方法說明:實現DocumentListener接口,必需實現的方法
*<br>輸入參數:
*<br>返回類型:
*/
public void insertUpdate(DocumentEvent e) {
enableButton();
}
/**
*<br>方法說明:實現DocumentListener接口,必需實現的方法
*<br>輸入參數:
*<br>返回類型:
*/
public void removeUpdate(DocumentEvent e) {
handleEmptyTextField(e);
}
/**
*<br>方法說明:實現DocumentListener接口,必需實現的方法
*<br>輸入參數:
*<br>返回類型:
*/
public void changedUpdate(DocumentEvent e) {
if (!handleEmptyTextField(e)) {
enableButton();
}
}
/**
*<br>方法說明:按鈕使能
*<br>輸入參數:
*<br>返回類型:
*/
private void enableButton() {
if (!alreadyEnabled) {
button.setEnabled(true);
}
}
/**
*<br>方法說明:實現DocumentListener接口,必需實現的方法,修改按鈕的狀態
*<br>輸入參數:
*<br>返回類型:
*/
private boolean handleEmptyTextField(DocumentEvent e) {
if (e.getDocument().getLength() <= 0) {
button.setEnabled(false);
alreadyEnabled = false;
return true;
}
return false;
}
}
/**
*<br>方法說明:實現ListSelectionListener接口,必需實現的方法
*<br>輸入參數:
*<br>返回類型:
*/
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() == -1) {
fireButton.setEnabled(false);
} else {
fireButton.setEnabled(true);
}
}
}
/**
*<br>方法說明:主方法
*<br>輸入參數:
*<br>返回類型:
*/
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
//創建一個窗體
JFrame frame = new JFrame("ListDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//創建一個面版
JComponent newContentPane = new ListDemo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
//顯示窗體
frame.pack();
frame.setVisible(true);
}
}