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

📄 dialogdemo.java

📁 java关于实现对话框的小程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        radioButtons[2].setActionCommand(questionCommand);        radioButtons[3] = new JRadioButton("错误图标");        radioButtons[3].setActionCommand(errorCommand);        radioButtons[4] = new JRadioButton("警告图标");        radioButtons[4].setActionCommand(warningCommand);        radioButtons[5] = new JRadioButton("自定义图标");        radioButtons[5].setActionCommand(customCommand);        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();                //没有图标                if (command == plainCommand) {                    JOptionPane.showMessageDialog(frame,                                    "水煮鱼里不要放酸菜!",                                    "无图标",                                    JOptionPane.PLAIN_MESSAGE);                //信息图标                } else if (command == infoCommand) {                    JOptionPane.showMessageDialog(frame,                                    "水煮鱼里不要放酸菜!",                                    "信息图标",                                    JOptionPane.INFORMATION_MESSAGE);                //问题图标                } else if (command == questionCommand) {                    JOptionPane.showMessageDialog(frame,                                    "请你吃饭前洗手,好吗?",                                    "问题",                                    JOptionPane.QUESTION_MESSAGE);                //错误图标                } else if (command == errorCommand) {                    JOptionPane.showMessageDialog(frame,                                    "对不起,你的信用卡没有资金了!",                                    "错误信息",                                    JOptionPane.ERROR_MESSAGE);                //警告图标                } else if (command == warningCommand) {                    JOptionPane.showMessageDialog(frame,                                    "警告!你严重透支信用卡,请尽快补齐金额!",                                    "警告信息",                                    JOptionPane.WARNING_MESSAGE);                //自定义图标                } else if (command == customCommand) {                    JOptionPane.showMessageDialog(frame,                                    "哈哈。我想用什么图标都可以!",                                    "自定义对话窗",                                    JOptionPane.INFORMATION_MESSAGE,                                    icon);                }            }        });        return create2ColPane(iconDesc + ":",                              radioButtons,                              showItButton);    }/** *<br>方法说明:创建一个JPanel,放在第二个选项卡上 *<br>输入参数: *<br>返回类型: */    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";        //定义radio数组        radioButtons[0] = new JRadioButton("选择一个");        radioButtons[0].setActionCommand(pickOneCommand);        radioButtons[1] = new JRadioButton("输入信息");        radioButtons[1].setActionCommand(textEnteredCommand);        radioButtons[2] = new JRadioButton("关闭按钮无效");        radioButtons[2].setActionCommand(nonAutoCommand);        radioButtons[3] = new JRadioButton("输入校验"                                           + "(用户输入信息)");        radioButtons[3].setActionCommand(customOptionCommand);        radioButtons[4] = new JRadioButton("没有模式");        radioButtons[4].setActionCommand(nonModalCommand);        //合成一个组群        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();                //选择一个                if (command == pickOneCommand) {                    Object[] possibilities = {"辣椒", "西红柿", "洋葱"};                    //设置对话框                    String s = (String)JOptionPane.showInputDialog(                                        frame,    //所属窗体                                        "请选择项目:\n"                                        + "\"鸡蛋炒\"",  //输出信息                                        "客户选择",                                        JOptionPane.PLAIN_MESSAGE,  //对话框模式                                        icon,           //显示图标                                        possibilities,   //选项内容                                        "辣椒");    //默认选项                    //如果有选择                    if ((s != null) && (s.length() > 0)) {                        setLabel("鸡蛋炒" + s + "!");                        return;                    }                    //如果客户没有选择                    setLabel("快点!");                //文本输入                } else if (command == textEnteredCommand) {                    String s = (String)JOptionPane.showInputDialog(                                        frame,                                        "选择一个配料\n"                                        + "\"鸡蛋炒\"",                                        "客户输入",                                        JOptionPane.PLAIN_MESSAGE,                                        icon,                                        null,                                        "辣椒");                    //如果用户有输入                    if ((s != null) && (s.length() > 0)) {                        setLabel("你要的是鸡蛋炒" + s + "!");                        return;                    }                    //如果返回的是空或者是null。                    setLabel("快些选择!");                //关闭按钮无效                } else if (command == nonAutoCommand) {                    //构造一个对话框面板                    final JOptionPane optionPane = new JOptionPane(                                    "关闭这个对话框\n"                                    + "请点击下面的按钮\n"                                    + "明白吗?",                                    JOptionPane.QUESTION_MESSAGE,                                    JOptionPane.YES_NO_OPTION);                    JDialog.setDefaultLookAndFeelDecorated(false);                    //构造一个对话框                    final JDialog dialog = new JDialog(frame,                                                 "点击一个按钮",                                                 true);                    //将对话框面板添加到对话框中                    dialog.setContentPane(optionPane);                    //设置对话框关闭时的操作模式                    dialog.setDefaultCloseOperation(                        JDialog.DO_NOTHING_ON_CLOSE);                    dialog.addWindowListener(new WindowAdapter() {                        public void windowClosing(WindowEvent we) { //当点击关闭按钮                            setLabel("阻碍用户视图关闭窗体!");                        }                    });                                        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))) {                                    //如果你要阻止关闭按钮,可以在这里进行处理。                                                                        dialog.setVisible(false);                                }                            }                        });                    dialog.pack();                    dialog.setLocationRelativeTo(frame);                    dialog.setVisible(true);                                        int value = ((Integer)optionPane.getValue()).intValue();                    if (value == JOptionPane.YES_OPTION) {                        setLabel("好的");                    } else if (value == JOptionPane.NO_OPTION) {                        setLabel("试图点击关闭按钮来关闭一个不能关闭的对话框!"                                 + "你不能!");                    } else {                        setLabel("窗体可以使用ESC键关闭。");                    }                 //自己定义版面                } else if (command == customOptionCommand) {                    customDialog.setLocationRelativeTo(frame);                    customDialog.setVisible(true);                    String s = customDialog.getValidatedText();                    if (s != null) {                        //The text is valid.                        setLabel("欢迎你!"                                 + "你已经进入了\""                                 + s                                 + "\"。");                    }                //没有模式                } else if (command == nonModalCommand) {                    //创建一个对话框                    final JDialog dialog = new JDialog(frame,                                                       "一个没有模式的对话框");                    //使用html语言来显示信息                    JLabel label = new JLabel("<html><p align=center>"                        + "这是一个没有模式的对话框<br>"                        + "你可以使用更多的格式<br>"                        + "甚至可以使用主窗体!");                    label.setHorizontalAlignment(JLabel.CENTER);                    Font font = label.getFont();                                        label.setFont(label.getFont().deriveFont(font.PLAIN,                                                             14.0f));                    JButton closeButton = new JButton("关闭");                    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);                    //显示窗体                    dialog.setSize(new Dimension(300, 150));                    dialog.setLocationRelativeTo(frame);                    dialog.setVisible(true);                }            }        });        return createPane(moreDialogDesc + ":",                          radioButtons,                          showItButton);    }    public static void main(String[] args) {        JFrame.setDefaultLookAndFeelDecorated(true);        JDialog.setDefaultLookAndFeelDecorated(true);        //创建和设置一个窗体        JFrame frame = new JFrame("DialogDemo");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //设置一个面板        Container contentPane = frame.getContentPane();        contentPane.setLayout(new GridLayout(1,1));        contentPane.add(new DialogDemo(frame));        //显示窗体        frame.pack();        frame.setVisible(true);    }}

⌨️ 快捷键说明

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