Java消息提示框JOptionPane的使用方法

原文地址:http://blog.csdn.net/c1481118216/article/details/51921521

  • 首先顺带介绍下改变swing控件的风格为当前系统(windwos 10)风格的代码,以下的都是windows风格的提示框,个人觉得比java风格好看
try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • showMessaDialog(只有一个确认按钮的消息提示框)

这里写图片描述

JOptionPane.showMessageDialog(null, "我是普通提示框!╮(╯▽╰)╭");  
  
  
  • 1

这里写图片描述

JOptionPane.showMessageDialog(null, "我是警告提示框!╮(╯▽╰)╭", "标题",JOptionPane.WARNING_MESSAGE);  
  
  
  • 1

这里写图片描述

JOptionPane.showMessageDialog(null, "我是错误提示框!╮(╯▽╰)╭", "标题",JOptionPane.ERROR_MESSAGE); 
  
  
  • 1

这里写图片描述

JOptionPane.showMessageDialog(null, "我是最基本提示框!╮(╯▽╰)╭", "标题",JOptionPane.PLAIN_MESSAGE);
  
  
  • 1
  • showOptionDialog(带有自定义选择按钮的选择提示框,按钮和提示消息均可自定义)

这里写图片描述

int n = JOptionPane.showConfirmDialog(null, "你会了吗?", "标题",JOptionPane.YES_NO_OPTION); //返回值为0或1
  
  
  • 1

这里写图片描述

Object[] options ={ "必须是", "当然是" };  //自定义按钮上的文字
int m = JOptionPane.showOptionDialog(null, "钓鱼岛是中国的吗?", "标题",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);  
  
  
  • 1
  • 2
  • showInoutDialog(带有下拉选择列表或者输入框的提示框,并可以将用户选择或者输入信息返回)

这里写图片描述

Object[] obj2 ={ "路人甲", "路人乙", "路人丙" };  
String s = (String) JOptionPane.showInputDialog(null,"请选择你的身份:\n", "身份", JOptionPane.PLAIN_MESSAGE, new ImageIcon("icon.png"), obj2, "足球");
  
  
  • 1
  • 2

这里写图片描述

JOptionPane.showInputDialog(null,"请输入:\n","title",JOptionPane.PLAIN_MESSAGE); 

猜你喜欢

转载自blog.csdn.net/zxgmdzz/article/details/78350973