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

📄 dialogdemo.java

📁 java tutorial.sun公司官方出品。java入门书籍。最新版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * *   - Redistributions of source code must retain the above copyright *     notice, this list of conditions and the following disclaimer. * *   - Redistributions in binary form must reproduce the above copyright *     notice, this list of conditions and the following disclaimer in the *     documentation and/or other materials provided with the distribution. * *   - Neither the name of Sun Microsystems nor the names of its *     contributors may be used to endorse or promote products derived *     from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package components;import javax.swing.JOptionPane;import javax.swing.JDialog;import javax.swing.JButton;import javax.swing.JRadioButton;import javax.swing.ButtonGroup;import javax.swing.JLabel;import javax.swing.ImageIcon;import javax.swing.BoxLayout;import javax.swing.Box;import javax.swing.BorderFactory;import javax.swing.border.Border;import javax.swing.JTabbedPane;import javax.swing.JPanel;import javax.swing.JFrame;import java.beans.*; //Property change stuffimport java.awt.*;import java.awt.event.*;/* * DialogDemo.java requires these files: *   CustomDialog.java *   images/middle.gif */public class DialogDemo extends JPanel {    JLabel label;    ImageIcon icon = createImageIcon("images/middle.gif");    JFrame frame;    String simpleDialogDesc = "Some simple message dialogs";    String iconDesc = "A JOptionPane has its choice of icons";    String moreDialogDesc = "Some more dialogs";    CustomDialog customDialog;    /** Creates the GUI shown inside the frame's content pane. */    public DialogDemo(JFrame frame) {        super(new BorderLayout());        this.frame = frame;        customDialog = new CustomDialog(frame, "geisel", this);        customDialog.pack();        //Create the components.        JPanel frequentPanel = createSimpleDialogBox();        JPanel featurePanel = createFeatureDialogBox();        JPanel iconPanel = createIconDialogBox();        label = new JLabel("Click the \"Show it!\" button"                           + " to bring up the selected dialog.",                           JLabel.CENTER);        //Lay them out.        Border padding = BorderFactory.createEmptyBorder(20,20,5,20);        frequentPanel.setBorder(padding);        featurePanel.setBorder(padding);        iconPanel.setBorder(padding);        JTabbedPane tabbedPane = new JTabbedPane();        tabbedPane.addTab("Simple Modal Dialogs", null,                          frequentPanel,                          simpleDialogDesc); //tooltip text        tabbedPane.addTab("More Dialogs", null,                          featurePanel,                          moreDialogDesc); //tooltip text        tabbedPane.addTab("Dialog Icons", null,                          iconPanel,                          iconDesc); //tooltip text        add(tabbedPane, BorderLayout.CENTER);        add(label, BorderLayout.PAGE_END);        label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));    }    /** Sets the text displayed at the bottom of the frame. */    void setLabel(String newText) {        label.setText(newText);    }    /** Returns an ImageIcon, or null if the path was invalid. */    protected static ImageIcon createImageIcon(String path) {        java.net.URL imgURL = DialogDemo.class.getResource(path);        if (imgURL != null) {            return new ImageIcon(imgURL);        } else {            System.err.println("Couldn't find file: " + path);            return null;        }    }    /** Creates the panel shown by the first tab. */    private JPanel createSimpleDialogBox() {        final int numButtons = 4;        JRadioButton[] radioButtons = new JRadioButton[numButtons];        final ButtonGroup group = new ButtonGroup();        JButton showItButton = null;        final String defaultMessageCommand = "default";        final String yesNoCommand = "yesno";        final String yeahNahCommand = "yeahnah";        final String yncCommand = "ync";        radioButtons[0] = new JRadioButton("OK (in the L&F's words)");        radioButtons[0].setActionCommand(defaultMessageCommand);        radioButtons[1] = new JRadioButton("Yes/No (in the L&F's words)");        radioButtons[1].setActionCommand(yesNoCommand);        radioButtons[2] = new JRadioButton("Yes/No "                      + "(in the programmer's words)");        radioButtons[2].setActionCommand(yeahNahCommand);        radioButtons[3] = new JRadioButton("Yes/No/Cancel "                           + "(in the programmer's words)");        radioButtons[3].setActionCommand(yncCommand);        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();                //ok dialog                if (command == defaultMessageCommand) {                    JOptionPane.showMessageDialog(frame,                                "Eggs aren't supposed to be green.");                //yes/no dialog                } else if (command == yesNoCommand) {                    int n = JOptionPane.showConfirmDialog(                            frame, "Would you like green eggs and ham?",                            "An Inane Question",                            JOptionPane.YES_NO_OPTION);                    if (n == JOptionPane.YES_OPTION) {                        setLabel("Ewww!");                    } else if (n == JOptionPane.NO_OPTION) {                        setLabel("Me neither!");                    } else {                        setLabel("Come on -- tell me!");                    }                //yes/no (not in those words)                } else if (command == yeahNahCommand) {                    Object[] options = {"Yes, please", "No way!"};                    int n = JOptionPane.showOptionDialog(frame,                                    "Would you like green eggs and ham?",                                    "A Silly Question",                                    JOptionPane.YES_NO_OPTION,                                    JOptionPane.QUESTION_MESSAGE,                                    null,                                    options,                                    options[0]);                    if (n == JOptionPane.YES_OPTION) {                        setLabel("You're kidding!");                    } else if (n == JOptionPane.NO_OPTION) {                        setLabel("I don't like them, either.");                    } else {                        setLabel("Come on -- 'fess up!");                    }                //yes/no/cancel (not in those words)                } else if (command == yncCommand) {                    Object[] options = {"Yes, please",                                        "No, thanks",                                        "No eggs, no ham!"};                    int n = JOptionPane.showOptionDialog(frame,                                    "Would you like some green eggs to go "                                    + "with that ham?",                                    "A Silly Question",                                    JOptionPane.YES_NO_CANCEL_OPTION,                                    JOptionPane.QUESTION_MESSAGE,                                    null,                                    options,                                    options[2]);                    if (n == JOptionPane.YES_OPTION) {                        setLabel("Here you go: green eggs and ham!");                    } else if (n == JOptionPane.NO_OPTION) {                        setLabel("OK, just the ham, then.");                    } else if (n == JOptionPane.CANCEL_OPTION) {                        setLabel("Well, I'm certainly not going to eat them!");                    } else {                        setLabel("Please tell me what you want!");                    }                }                return;            }        });        return createPane(simpleDialogDesc + ":",                          radioButtons,                          showItButton);    }    /**     * Used by createSimpleDialogBox and createFeatureDialogBox     * to create a pane containing a description, a single column     * of radio buttons, and the Show it! button.     */    private JPanel createPane(String description,                              JRadioButton[] radioButtons,                              JButton showButton) {        int numChoices = radioButtons.length;        JPanel box = new JPanel();        JLabel label = new JLabel(description);        box.setLayout(new BoxLayout(box, BoxLayout.PAGE_AXIS));        box.add(label);        for (int i = 0; i < numChoices; i++) {            box.add(radioButtons[i]);        }        JPanel pane = new JPanel(new BorderLayout());        pane.add(box, BorderLayout.PAGE_START);        pane.add(showButton, BorderLayout.PAGE_END);        return pane;    }    /**     * Like createPane, but creates a pane with 2 columns of radio     * buttons.  The number of buttons passed in *must* be even.     */     private JPanel create2ColPane(String description,                                  JRadioButton[] radioButtons,                                  JButton showButton) {        JLabel label = new JLabel(description);        int numPerColumn = radioButtons.length/2;        JPanel grid = new JPanel(new GridLayout(0, 2));        for (int i = 0; i < numPerColumn; i++) {            grid.add(radioButtons[i]);            grid.add(radioButtons[i + numPerColumn]);        }        JPanel box = new JPanel();        box.setLayout(new BoxLayout(box, BoxLayout.PAGE_AXIS));        box.add(label);        grid.setAlignmentX(0.0f);        box.add(grid);        JPanel pane = new JPanel(new BorderLayout());        pane.add(box, BorderLayout.PAGE_START);        pane.add(showButton, BorderLayout.PAGE_END);        return pane;    }    /*     * Creates the panel shown by the 3rd tab.     * These dialogs are implemented using showMessageDialog, but     * you can specify the icon (using similar code) for any other     * kind of dialog, as well.     */    private JPanel createIconDialogBox() {        JButton showItButton = null;        final int numButtons = 6;        JRadioButton[] radioButtons = new JRadioButton[numButtons];        final ButtonGroup group = new ButtonGroup();        final String plainCommand = "plain";        final String infoCommand = "info";        final String questionCommand = "question";        final String errorCommand = "error";        final String warningCommand = "warning";        final String customCommand = "custom";        radioButtons[0] = new JRadioButton("Plain (no icon)");        radioButtons[0].setActionCommand(plainCommand);        radioButtons[1] = new JRadioButton("Information icon");        radioButtons[1].setActionCommand(infoCommand);        radioButtons[2] = new JRadioButton("Question icon");        radioButtons[2].setActionCommand(questionCommand);

⌨️ 快捷键说明

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