import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

public class ListDemo extends JPanel

implements ListSelectionListener {

private JList list;

private DefaultListModel listModel;

private static final String buyString = " 买";

private static final String sellString = "卖";

private JButton sellButton;

private JTextField fruit;

public ListDemo() {

super(new BorderLayout());

listModel = new DefaultListModel();

listModel.addElement("苹果");

listModel.addElement("香蕉");

listModel.addElement("雪梨");

listModel.addElement("西红柿");

listModel.addElement("西瓜");

listModel.addElement("水蜜桃");

//建立一个列表，并把它放入滚条面板里

list = new JList(listModel);

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

list.setSelectedIndex(0);

list.addListSelectionListener(this);

list.setVisibleRowCount(5);

JScrollPane listScrollPane = new JScrollPane(list);

JButton hireButton = new JButton(buyString);

HireListener hireListener = new HireListener(hireButton);

hireButton.setActionCommand(buyString);

hireButton.addActionListener(hireListener);

hireButton.setEnabled(false);

sellButton = new JButton(sellString);

sellButton.setActionCommand(sellString);

sellButton.addActionListener(new FireListener());

fruit = new JTextField(10);

fruit.addActionListener(hireListener);

fruit.getDocument().addDocumentListener(hireListener);

String name = listModel.getElementAt(

list.getSelectedIndex()).toString();

//创建一个面板，使用 BoxLayout 布局.

JPanel buttonPane = new JPanel();

buttonPane.setLayout(new BoxLayout(buttonPane,

BoxLayout.LINE_AXIS));

buttonPane.add(sellButton);

buttonPane.add(Box.createHorizontalStrut(5));

buttonPane.add(new JSeparator(SwingConstants.VERTICAL));

buttonPane.add(Box.createHorizontalStrut(5));

buttonPane.add(fruit);

buttonPane.add(hireButton);

buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

add(listScrollPane, BorderLayout.CENTER);

add(buttonPane, BorderLayout.PAGE_END);

}

class FireListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

//这个方法只有在有效选择的情况下才被调用

int index = list.getSelectedIndex();

listModel.remove(index);

int size = listModel.getSize();

if (size == 0) { //没有合适的选项了，则使“卖”按钮不可用

sellButton.setEnabled(false);

} else { //选择一个索引

if (index == listModel.getSize()) {

//从后面开始删除选项

index--;

}

list.setSelectedIndex(index);

list.ensureIndexIsVisible(index);

}

}

}

//这个监听器被文本域和“买”按钮共有

class HireListener implements ActionListener, DocumentListener {

private boolean alreadyEnabled = false;

private JButton button;

public HireListener(JButton button) {

this.button = button;

}

//动作监听器所必需的

public void actionPerformed(ActionEvent e) {

String name = fruit.getText();

//用户没有输入单一的名字。。

if (name.equals("") || alreadyInList(name)) {

Toolkit.getDefaultToolkit().beep();

fruit.requestFocusInWindow();

fruit.selectAll();

return;

}

int index = list.getSelectedIndex(); //取得选择索引

if (index == -1) { //没有选择，则在开头部位插入

index = 0;

} else { //在被选项后插入

index++;

}

listModel.insertElementAt(fruit.getText(), index);

//如果只想在后面插入，可使用下面的方法：

//listModel.addElement(fruit.getText());

//重置文本域

fruit.requestFocusInWindow();

fruit.setText("");

//选择新的选项，并让它可见

list.setSelectedIndex(index);

list.ensureIndexIsVisible(index);

}

//这个方法用来测试字串存在与否。

protected boolean alreadyInList(String name) {

return listModel.contains(name);

}

//DocumentListener 所必须.

public void insertUpdate(DocumentEvent e) {

enableButton();

}

//DocumentListener 所必须.

public void removeUpdate(DocumentEvent e) {

handleEmptyTextField(e);

}

//DocumentListener 所必须.

public void changedUpdate(DocumentEvent e) {

if (!handleEmptyTextField(e)) {

enableButton();

}

}

private void enableButton() {

if (!alreadyEnabled) {

button.setEnabled(true);

}

}

private boolean handleEmptyTextField(DocumentEvent e) {

if (e.getDocument().getLength() <= 0) {

button.setEnabled(false);

alreadyEnabled = false;

return true;

}

return false;

}

}

//ListSelectionListener 所必须.

public void valueChanged(ListSelectionEvent e) {

if (e.getValueIsAdjusting() == false) {

if (list.getSelectedIndex() == -1) {

//没有选择，使“卖”按钮不可用

sellButton.setEnabled(false);

} else {

//有选择，使“卖”按钮可用

sellButton.setEnabled(true);

}

}

}

/**

* 建立并显示这个 GUI。由事件分配线程来调用。

*/

private static void createAndShowGUI() {

//创建并设置这个窗口 Create and set up the window.

JFrame frame = new JFrame("ListDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//创建并设置 content 面板.

JComponent newContentPane = new ListDemo();

newContentPane.setOpaque(true); //content 将不是透明的

frame.setContentPane(newContentPane);

//显示这个窗口

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

//给事件分配管理器一个作业:

//建立并显示这个应用的 GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}