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

📄 listdialog.java

📁 这是一个英文版的《Java程序设计与问题解决》现在好多大学都当成教材
💻 JAVA
字号:
import javax.swing.*;import java.awt.*;import java.awt.event.*;/* * ListDialog.java is a 1.4 class meant to be used by programs such as * ListDialogRunner.  It requires no additional files. *//** * Use this modal dialog to let the user choose one string from a long * list.  See ListDialogRunner.java for an example of using ListDialog. * The basics: * <pre>    String[] choices = {"A", "long", "array", "of", "strings"};    String selectedName = ListDialog.showDialog(                                componentInControllingFrame,                                locatorComponent,                                "A description of the list:",                                "Dialog Title",                                choices,                                choices[0]); * </pre> */public class ListDialog extends JDialog                        implements ActionListener {    private static ListDialog dialog;    private static String value = "";    private JList list;    /**     * Set up and show the dialog.  The first Component argument     * determines which frame the dialog depends on; it should be     * a component in the dialog's controlling frame. The second     * Component argument should be null if you want the dialog     * to come up with its left corner in the center of the screen;     * otherwise, it should be the component on top of which the     * dialog should appear.     */    public static String showDialog(Component frameComp,                                    Component locationComp,                                    String labelText,                                    String title,                                    String[] possibleValues,                                    String initialValue) {        Frame frame = JOptionPane.getFrameForComponent(frameComp);        dialog = new ListDialog(frame,                                locationComp,                                labelText,                                title,                                possibleValues,                                initialValue);        dialog.setVisible(true);        return value;    }    private void setValue(String newValue) {        value = newValue;        list.setSelectedValue(value, true);    }    private ListDialog(Frame frame,                       Component locationComp,                       String labelText,                       String title,                       Object[] data,                       String initialValue) {        super(frame, title, true);        //Create and initialize the buttons.        JButton cancelButton = new JButton("Cancel");        cancelButton.addActionListener(this);        //        final JButton setButton = new JButton("Set");        setButton.setActionCommand("Set");        setButton.addActionListener(this);        getRootPane().setDefaultButton(setButton);        //main part of the dialog        list = new JList(data);        list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);        list.addMouseListener(new MouseAdapter() {            public void mouseClicked(MouseEvent e) {                if (e.getClickCount() == 2) {                    setButton.doClick(); //emulate button click                }            }        });        JScrollPane listScroller = new JScrollPane(list);        listScroller.setPreferredSize(new Dimension(250, 80));        //XXX: Must do the following, too, or else the scroller thinks        //XXX: it's taller than it is:        listScroller.setMinimumSize(new Dimension(250, 80));        listScroller.setAlignmentX(LEFT_ALIGNMENT);        //Create a container so that we can add a title around        //the scroll pane.  Can't add a title directly to the        //scroll pane because its background would be white.        //Lay out the label and scroll pane from top to button.        JPanel listPane = new JPanel();        listPane.setLayout(new BoxLayout(listPane, BoxLayout.Y_AXIS));        JLabel label = new JLabel(labelText);        label.setLabelFor(list);        listPane.add(label);        listPane.add(Box.createRigidArea(new Dimension(0,5)));        listPane.add(listScroller);        listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));        //Lay out the buttons from left to right.        JPanel buttonPane = new JPanel();        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));        buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));        buttonPane.add(Box.createHorizontalGlue());        buttonPane.add(cancelButton);        buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));        buttonPane.add(setButton);        //Put everything together, using the content pane's BorderLayout.        Container contentPane = getContentPane();        contentPane.add(listPane, BorderLayout.CENTER);        contentPane.add(buttonPane, BorderLayout.PAGE_END);        //Initialize values.        setValue(initialValue);        pack();        setLocationRelativeTo(locationComp);    }    //Handle clicks on the Set and Cancel buttons.    public void actionPerformed(ActionEvent e) {        if ("Set".equals(e.getActionCommand())) {            ListDialog.value = (String)(list.getSelectedValue());        }        ListDialog.dialog.setVisible(false);    }}

⌨️ 快捷键说明

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