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

📄 dialogdemo.java

📁 Java样例程序集合:2D
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                    "A plain message",                                    JOptionPane.PLAIN_MESSAGE);                //information icon                } else if (command == infoCommand) {                    JOptionPane.showMessageDialog(frame,                                    "Eggs aren't supposed to be green.",                                    "Inane informational dialog",                                    JOptionPane.INFORMATION_MESSAGE);            //XXX: It doesn't make sense to make a question with            //XXX: only one button.            //XXX: See "Yes/No (but not in those words)" for a better solution.                //question icon                } else if (command == questionCommand) {                    JOptionPane.showMessageDialog(frame,                                    "You shouldn't use a message dialog "                                    + "(like this)\n"                                    + "for a question, OK?",                                    "Inane question",                                    JOptionPane.QUESTION_MESSAGE);                //error icon                } else if (command == errorCommand) {                    JOptionPane.showMessageDialog(frame,                                    "Eggs aren't supposed to be green.",                                    "Inane error",                                    JOptionPane.ERROR_MESSAGE);                //warning icon                } else if (command == warningCommand) {                    JOptionPane.showMessageDialog(frame,                                    "Eggs aren't supposed to be green.",                                    "Inane warning",                                    JOptionPane.WARNING_MESSAGE);                //custom icon                } else if (command == customCommand) {                    JOptionPane.showMessageDialog(frame,                                    "Eggs aren't supposed to be green.",                                    "Inane custom dialog",                                    JOptionPane.INFORMATION_MESSAGE,                                    icon);                }            }        });        return create2ColPane(iconDesc + ":",                              radioButtons,                              showItButton);    }    /** Creates the panel shown by the second tab. */    private JPanel createFeatureDialogBox() {        final int numButtons = 5;        JRadioButton[] radioButtons = new JRadioButton[numButtons];        final ButtonGroup group = new ButtonGroup();        JButton showItButton = null;        final String pickOneCommand = "pickone";        final String textEnteredCommand = "textfield";        final String nonAutoCommand = "nonautooption";        final String customOptionCommand = "customoption";        final String nonModalCommand = "nonmodal";        radioButtons[0] = new JRadioButton("Pick one of several choices");        radioButtons[0].setActionCommand(pickOneCommand);        radioButtons[1] = new JRadioButton("Enter some text");        radioButtons[1].setActionCommand(textEnteredCommand);        radioButtons[2] = new JRadioButton("Non-auto-closing dialog");        radioButtons[2].setActionCommand(nonAutoCommand);        radioButtons[3] = new JRadioButton("Input-validating dialog "                                           + "(with custom message area)");        radioButtons[3].setActionCommand(customOptionCommand);        radioButtons[4] = new JRadioButton("Non-modal dialog");        radioButtons[4].setActionCommand(nonModalCommand);        for (int i = 0; i < numButtons; i++) {            group.add(radioButtons[i]);        }        radioButtons[0].setSelected(true);        showItButton = new JButton("Show it!");        showItButton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                String command = group.getSelection().getActionCommand();                //pick one of many                if (command == pickOneCommand) {                    Object[] possibilities = {"ham", "spam", "yam"};                    String s = (String)JOptionPane.showInputDialog(                                        frame,                                        "Complete the sentence:\n"                                        + "\"Green eggs and...\"",                                        "Customized Dialog",                                        JOptionPane.PLAIN_MESSAGE,                                        icon,                                        possibilities,                                        "ham");                    //If a string was returned, say so.                    if ((s != null) && (s.length() > 0)) {                        setLabel("Green eggs and... " + s + "!");                        return;                    }                    //If you're here, the return value was null/empty.                    setLabel("Come on, finish the sentence!");                //text input                } else if (command == textEnteredCommand) {                    String s = (String)JOptionPane.showInputDialog(                                        frame,                                        "Complete the sentence:\n"                                        + "\"Green eggs and...\"",                                        "Customized Dialog",                                        JOptionPane.PLAIN_MESSAGE,                                        icon,                                        null,                                        "ham");                    //If a string was returned, say so.                    if ((s != null) && (s.length() > 0)) {                        setLabel("Green eggs and... " + s + "!");                        return;                    }                    //If you're here, the return value was null/empty.                    setLabel("Come on, finish the sentence!");                //non-auto-closing dialog                } else if (command == nonAutoCommand) {                    final JOptionPane optionPane = new JOptionPane(                                    "The only way to close this dialog is by\n"                                    + "pressing one of the following buttons.\n"                                    + "Do you understand?",                                    JOptionPane.QUESTION_MESSAGE,                                    JOptionPane.YES_NO_OPTION);                    //HACK used to get a close button.  This dialog                    //illustrates the questionable practice of not                    //letting someone close the window using the                    //window decoration.  Since the Java Look and Feel                    //doesn't decorate its dialogs with a close button,                    //we must turn on system window decorations in hopes                    //of getting a close button.                    JDialog.setDefaultLookAndFeelDecorated(false);                    //You can't use pane.createDialog() because that                    //method sets up the JDialog with a property change                    //listener that automatically closes the window                    //when a button is clicked.                    final JDialog dialog = new JDialog(frame,                                                 "Click a button",                                                 true);                    dialog.setContentPane(optionPane);                    dialog.setDefaultCloseOperation(                        JDialog.DO_NOTHING_ON_CLOSE);                    dialog.addWindowListener(new WindowAdapter() {                        public void windowClosing(WindowEvent we) {                            setLabel("Thwarted user attempt to close window.");                        }                    });                    //Undo the hack!                    JDialog.setDefaultLookAndFeelDecorated(true);                    optionPane.addPropertyChangeListener(                        new PropertyChangeListener() {                            public void propertyChange(PropertyChangeEvent e) {                                String prop = e.getPropertyName();                                if (dialog.isVisible()                                 && (e.getSource() == optionPane)                                 && (JOptionPane.VALUE_PROPERTY.equals(prop))) {                                    //If you were going to check something                                    //before closing the window, you'd do                                    //it here.                                    dialog.setVisible(false);                                }                            }                        });                    dialog.pack();                    dialog.setLocationRelativeTo(frame);                    dialog.setVisible(true);                    int value = ((Integer)optionPane.getValue()).intValue();                    if (value == JOptionPane.YES_OPTION) {                        setLabel("Good.");                    } else if (value == JOptionPane.NO_OPTION) {                        setLabel("Try using the window decorations "                                 + "to close the non-auto-closing dialog. "                                 + "You can't!");                    } else {                        setLabel("Window unavoidably closed (ESC?).");                    }                //non-auto-closing dialog with custom message area                //NOTE: if you don't intend to check the input,                //then just use showInputDialog instead.                } else if (command == customOptionCommand) {                    customDialog.setLocationRelativeTo(frame);                    customDialog.setVisible(true);                    String s = customDialog.getValidatedText();                    if (s != null) {                        //The text is valid.                        setLabel("Congratulations!  "                                 + "You entered \""                                 + s                                 + "\".");                    }                //non-modal dialog                } else if (command == nonModalCommand) {                    //Create the dialog.                    final JDialog dialog = new JDialog(frame,                                                       "A Non-Modal Dialog");                    //Add contents to it. It must have a close button,                    //since some L&Fs (notably Java/Metal) don't provide one                    //in the window decorations for dialogs.                    JLabel label = new JLabel("<html><p align=center>"                        + "This is a non-modal dialog.<br>"                        + "You can have one or more of these up<br>"                        + "and still use the main window.");                    label.setHorizontalAlignment(JLabel.CENTER);                    Font font = label.getFont();                    label.setFont(label.getFont().deriveFont(font.PLAIN,                                                             14.0f));                    JButton closeButton = new JButton("Close");                    closeButton.addActionListener(new ActionListener() {                        public void actionPerformed(ActionEvent e) {                            dialog.setVisible(false);                            dialog.dispose();                        }                    });                    JPanel closePanel = new JPanel();                    closePanel.setLayout(new BoxLayout(closePanel,                                                       BoxLayout.LINE_AXIS));                    closePanel.add(Box.createHorizontalGlue());                    closePanel.add(closeButton);                    closePanel.setBorder(BorderFactory.                        createEmptyBorder(0,0,5,5));                    JPanel contentPane = new JPanel(new BorderLayout());                    contentPane.add(label, BorderLayout.CENTER);                    contentPane.add(closePanel, BorderLayout.PAGE_END);                    contentPane.setOpaque(true);                    dialog.setContentPane(contentPane);                    //Show it.                    dialog.setSize(new Dimension(300, 150));                    dialog.setLocationRelativeTo(frame);                    dialog.setVisible(true);                }            }        });        return createPane(moreDialogDesc + ":",                          radioButtons,                          showItButton);    }    /**     * Create the GUI and show it.  For thread safety,     * this method should be invoked from the     * event-dispatching thread.     */    private static void createAndShowGUI() {        //Make sure we have nice window decorations.        JFrame.setDefaultLookAndFeelDecorated(true);        //Create and set up the window.        JFrame frame = new JFrame("DialogDemo");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //Create and set up the content pane.        DialogDemo newContentPane = new DialogDemo(frame);        newContentPane.setOpaque(true); //content panes must be opaque        frame.setContentPane(newContentPane);        //Display the window.        frame.pack();        frame.setVisible(true);    }    public static void main(String[] args) {        //Schedule a job for the event-dispatching thread:        //creating and showing this application's GUI.        javax.swing.SwingUtilities.invokeLater(new Runnable() {            public void run() {                createAndShowGUI();            }        });    }}

⌨️ 快捷键说明

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