⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listdemo.java

📁 这是一个英文版的《Java程序设计与问题解决》现在好多大学都当成教材
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;/* ListDemo.java requires no other files. */public class ListDemo extends JPanel                      implements ListSelectionListener {    private JList list;    private DefaultListModel listModel;    private static final String hireString = "Hire";    private static final String fireString = "Fire";    private JButton fireButton;    private JTextField employeeName;    public ListDemo() {        listModel = new DefaultListModel();        listModel.addElement("Alison Huml");        listModel.addElement("Kathy Walrath");        listModel.addElement("Lisa Friendly");        listModel.addElement("Mary Campione");        listModel.addElement("Sharon Zakhour");        listModel.addElement("Alan Sommerer");        //Create the list and put it in a scroll pane        list = new JList(listModel);        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);        list.setSelectedIndex(0);        list.addListSelectionListener(this);        JScrollPane listScrollPane = new JScrollPane(list);        JButton hireButton = new JButton(hireString);        hireButton.setActionCommand(hireString);        hireButton.addActionListener(new HireListener());        fireButton = new JButton(fireString);        fireButton.setActionCommand(fireString);        fireButton.addActionListener(new FireListener());        employeeName = new JTextField(10);        employeeName.addActionListener(new HireListener());        String name = listModel.getElementAt(                              list.getSelectedIndex()).toString();        employeeName.setText(name);        //Create a panel that uses FlowLayout (the default).        JPanel buttonPane = new JPanel();        buttonPane.add(employeeName);        buttonPane.add(hireButton);        buttonPane.add(fireButton);        setLayout(new BorderLayout());        add(listScrollPane, BorderLayout.CENTER);        add(buttonPane, BorderLayout.PAGE_END);    }    class FireListener implements ActionListener {        public void actionPerformed(ActionEvent e) {            //This method can be called only if            //there's a valid selection            //so go ahead and remove whatever's selected.            int index = list.getSelectedIndex();            listModel.remove(index);            int size = listModel.getSize();            if (size == 0) {            //Nobody's left, disable firing.                fireButton.setEnabled(false);            } else {            //Adjust the selection.                if (index == listModel.getSize())//removed item in last position                    index--;                list.setSelectedIndex(index);   //otherwise select same index            }        }    }    //This listener is shared by the text field and the hire button    class HireListener implements ActionListener {        public void actionPerformed(ActionEvent e) {            String name = employeeName.getText();            //User didn't type in a unique name...            if (name.equals("") || alreadyInList(name)) {                Toolkit.getDefaultToolkit().beep();                return;            }            int index = list.getSelectedIndex();            int size = listModel.getSize();            //If no selection or if item in last position is selected,            //add the new hire to end of list, and select new hire.            if (index == -1 || (index+1 == size)) {                listModel.addElement(employeeName.getText());                list.setSelectedIndex(size);            //Otherwise insert the new hire after the current selection,            //and select new hire.            } else {                listModel.insertElementAt(employeeName.getText(), index+1);                list.setSelectedIndex(index+1);            }        }        //This method tests for string equality. You could certainly        //get more sophisticated about the algorithm.  For example,        //you might want to ignore white space and capitalization.        protected boolean alreadyInList(String name) {            return listModel.contains(name);        }    }    public void valueChanged(ListSelectionEvent e) {        if (e.getValueIsAdjusting() == false) {            if (list.getSelectedIndex() == -1) {            //No selection, disable fire button.                fireButton.setEnabled(false);                employeeName.setText("");            } else {            //Selection, update text field.                fireButton.setEnabled(true);                String name = list.getSelectedValue().toString();                employeeName.setText(name);            }        }    }    public static void main(String[] args) {        //Make sure we have nice window decorations.        JFrame.setDefaultLookAndFeelDecorated(true);        //Create and set up the window.        JFrame frame = new JFrame("ListDemo");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setContentPane(new ListDemo());        //Display the window.        frame.pack();        frame.setVisible(true);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -