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

📄 dialogdemo.java

📁 java关于实现对话框的小程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
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.*; import java.awt.*;import java.awt.event.*;/** * <p>Title: 对话框演示</p> * <p>Description: 全面的演示各种类型的对话框的使用</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Filename: DialogDemo.java</p> * @author 杜江 * @version 1.0 */public class DialogDemo extends JPanel {    JLabel label;    ImageIcon icon = createImageIcon("images/middle.gif");    JFrame frame;    String simpleDialogDesc = "简单的信息提示对话窗";    String iconDesc = "带有图标的对话窗";    String moreDialogDesc = "复杂信息对话窗";    CustomDialog customDialog;/** *<br>方法说明:构造器,生成一个面板添加到JFrame中 *<br>输入参数: *<br>返回类型: */    public DialogDemo(JFrame frame) {        super(new BorderLayout());        this.frame = frame;        customDialog = new CustomDialog(frame, "tom", this);        customDialog.pack();        //创建成员        JPanel frequentPanel = createSimpleDialogBox();        JPanel featurePanel = createFeatureDialogBox();        JPanel iconPanel = createIconDialogBox();        label = new JLabel("点击\"显示\" 按钮"                           + " 显示一个选择的对话框",                           JLabel.CENTER);        //放置对象        Border padding = BorderFactory.createEmptyBorder(20,20,5,20);        frequentPanel.setBorder(padding);        featurePanel.setBorder(padding);        iconPanel.setBorder(padding);        //创建选项卡        JTabbedPane tabbedPane = new JTabbedPane();        tabbedPane.addTab("简单对话窗", null,                          frequentPanel,                          simpleDialogDesc);         tabbedPane.addTab("复杂对话窗", null,                          featurePanel,                          moreDialogDesc);        tabbedPane.addTab("图标对话窗", null,                          iconPanel,                          iconDesc);        add(tabbedPane, BorderLayout.CENTER);        add(label, BorderLayout.PAGE_END);        label.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));    }/** *<br>方法说明:设置按钮上的文字 *<br>输入参数:String newText 添加的文字 *<br>返回类型: */    void setLabel(String newText) {        label.setText(newText);    }/** *<br>方法说明:获取图片 *<br>输入参数:String path 图片完整路径和名字 *<br>返回类型:ImageIcon 图片对象 */    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;        }    }/** *<br>方法说明:创建一个JPanel,给第一个选项卡 *<br>输入参数: *<br>返回类型: */    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”按钮");        radioButtons[0].setActionCommand(defaultMessageCommand);        radioButtons[1] = new JRadioButton("有“Yes/No”二个按钮");        radioButtons[1].setActionCommand(yesNoCommand);        radioButtons[2] = new JRadioButton("有“Yes/No”两个按钮 "                      + "(程序添加文字)");        radioButtons[2].setActionCommand(yeahNahCommand);        radioButtons[3] = new JRadioButton("有“Yes/No/Cancel”三个按钮 "                           + "(程序添加文字)");        radioButtons[3].setActionCommand(yncCommand);        //将四个单选组成一个群        for (int i = 0; i < numButtons; i++) {            group.add(radioButtons[i]);        }        //设置第一个为默认选择        radioButtons[0].setSelected(true);        //定义“显示”按钮        showItButton = new JButton("显示");        //给“显示”按钮添加监听        showItButton.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {                String command = group.getSelection().getActionCommand();                //ok对话窗                if (command == defaultMessageCommand) {                    JOptionPane.showMessageDialog(frame,                                "鸡蛋不可能是绿色的!");                //yes/no 对话窗                } else if (command == yesNoCommand) {                    int n = JOptionPane.showConfirmDialog(                            frame, "你喜欢吃酸菜鱼吗?",                            "一个很无聊的问题!!",                            JOptionPane.YES_NO_OPTION);                    if (n == JOptionPane.YES_OPTION) {//选择yes                        setLabel("哇!我也是!");                    } else if (n == JOptionPane.NO_OPTION) {//选择no                        setLabel("唉!我喜欢吃!");                    } else {                        setLabel("快告诉我吧!");                    }                //yes/no (自己输入选项)                } else if (command == yeahNahCommand) {                    Object[] options = {"是的", "不喜欢"};                    int n = JOptionPane.showOptionDialog(frame,                                    "你喜欢酸菜鱼吗?",                                    "又一个无聊的问题!",                                    JOptionPane.YES_NO_OPTION,                                    JOptionPane.QUESTION_MESSAGE,                                    null,                                    options,                                    options[0]);                    if (n == JOptionPane.YES_OPTION) {                        setLabel("你哄人的吧,我也喜欢。");                    } else if (n == JOptionPane.NO_OPTION) {                        setLabel("其实我也不喜欢!");                    } else {                        setLabel("这都不肯告诉我,小气鬼!");                    }                //yes/no/cancel 对话框                } else if (command == yncCommand) {                    Object[] options = {"是的,给我来一份。",                                        "不,谢谢!",                                        "不,我要水煮鱼!"};                    //构造对话框                    int n = JOptionPane.showOptionDialog(frame,                                    "先生!我们这里有鲜美的酸菜鱼,您需要吗?",                                    "服务生的问题。",                                    JOptionPane.YES_NO_CANCEL_OPTION,                                    JOptionPane.QUESTION_MESSAGE,                                    null,                                    options,                                    options[2]);                    if (n == JOptionPane.YES_OPTION) {                        setLabel("你要的酸菜鱼来了!");                    } else if (n == JOptionPane.NO_OPTION) {                        setLabel("好的,你需要其它的。");                    } else if (n == JOptionPane.CANCEL_OPTION) {                        setLabel("好的,我们给你做水煮鱼!");                    } else {                        setLabel("对不起!你还没有点菜呢!");                    }                }                return;            }        });        return createPane(simpleDialogDesc + ":",                          radioButtons,                          showItButton);    }/** *<br>方法说明:提供给createSimpleDialogBox和createFeatureDialogBox方法 *<br>方法说明:创建带提示信息、一列单选框和“显示”按钮 *<br>输入参数:String description 提示帮助信息 *<br>输入参数:JRadioButton[] radioButtons 单选框组 *<br>输入参数:JButton showButton “显示”按钮 *<br>返回类型:JPanel 添加好的面板 */    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);        //添加radio        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;    }/** *<br>方法说明:提供给createSimpleDialogBox和createFeatureDialogBox方法 *<br>方法说明:创建带提示信息、二列单选框和“显示”按钮 *<br>输入参数:String description 提示帮助信息 *<br>输入参数:JRadioButton[] radioButtons 单选框组 *<br>输入参数:JButton showButton “显示”按钮 *<br>返回类型:JPanel 添加好的面板 */     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;    }/** *<br>方法说明:创建第三个选项卡的面板 *<br>方法说明:这里都是实现showMessageDialog类,但是也可以指定图标 *<br>输入参数: *<br>返回类型:JPanel 构造好的面板 */    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("普通(没有图标)");        radioButtons[0].setActionCommand(plainCommand);        radioButtons[1] = new JRadioButton("信息图标");        radioButtons[1].setActionCommand(infoCommand);        radioButtons[2] = new JRadioButton("问题图标");

⌨️ 快捷键说明

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