您的位置 首页 java

超详细的Java弹窗样式及使用教程

推荐阅读:

今天来和大家分享一个Swing程序设计中关于JOptionPane类的使用,该类的作用呢,其实主要就是设置弹窗,所以在这里也就和大家总结了常用的弹窗设置的方法以及JOptionPane类详细使用说明!

话不多说上教程!

JOptionPane类属于Swing组件中的一种,所以导入方式如下:

四种消息提示框

在该类中常用的常用的四种消息提示框为:

超详细的Java弹窗样式及使用教程

五种消息类型

并且每一种消息框都有五种不同的消息类型,消息类型不同时,弹窗所对应的图标也就不同,以下是这五种消息类型

超详细的Java弹窗样式及使用教程

在使用不同类型的消息框时,输入不同的消息类型参数,就可以得到相应的消息框。

九种对话框参数

每一种消息提示框都对应有不同的参数的方法,从而得到不同的效果,但总结起来,这四种消息提示框的方法中都基本需要这九个参数:

超详细的Java弹窗样式及使用教程

因为这四种对话框中所需要的参数都是以上九种,所以接下来对每一种对话框进行分析,

确认对话框

确认对话框(showConfirmDialog)有以下四种构造函数,其中的参数与上表相对应:

 1、JOptionPane.showConfirmDialog(parentComponent, message)2、JOptionPane.showConfirmDialog(parentComponent, message, title, optionType)3、JOptionPane.showConfirmDialog(parentComponent, message, title, optionType,messageType)4、JOptionPane.showConfirmDialog(parentComponent, message, title, optionType, messageType, icon)  

实例如下:

 JOptionPane.showConfirmDialog(null,"这是确认对话框吗?","提示",JOptionPane.OK_OPTION,JOptionPane.QUESTION_MESSAGE);//确认对话框  

效果如下:

超详细的Java弹窗样式及使用教程

对确认消息对话框消息进行接收的方法:

 int userOption =  JOptionPane.showConfirmDialog(null,"这是确认对话框吗?","提示",JOptionPane.OK_OPTION,JOptionPane.QUESTION_MESSAGE);//确认对话框//如果用户选择的是OKif (userOption == JOptionPane.OK_OPTION) {System.err.println("是");}else {System.out.println("否");}  

输入对话框

输入对话框(showInputDialog)有六种构造函数,分别如下:

 1、JOptionPane.showInputDialog(message);2、JOptionPane.showInputDialog(parentComponent, message);3、JOptionPane.showInputDialog(message, initialSelectionValue);4、JOptionPane.showInputDialog(parentComponent,message,initialSelectionValue)5、JOptionPane.showInputDialog(parentComponent,message, title, messageType);6、JOptionPane.showInputDialog(parentComponent, message, title, messageType, icon, selectionValues, initialSelectionValue)  

下面有两个关于该对话框的实例:

1、显示输入框,供用户输入,实例如下:

 JOptionPane.showInputDialog(null,"请输入你的生日:","输入",JOptionPane.WARNING_MESSAGE);//输入对话框  

效果如下:

超详细的Java弹窗样式及使用教程

普通输入框情况下获取用户输入内容的方法:

 String info = JOptionPane.showInputDialog(null,"请输入你的生日:","输入",JOptionPane.WARNING_MESSAGE);//输入对话框System.out.println(info);  

2、设置一个下拉框,供用户选择输入,最后一个参数表示下拉框默认显示的内容,实例如下:

 String [] options = {"A选项","B选项","C选项","D选项"};JOptionPane.showInputDialog(null,"请输入你的选项:","提示",JOptionPane.QUESTION_MESSAGE,null,options,options[2]);  

效果如下:

超详细的Java弹窗样式及使用教程

下拉框情况下获取用户输入内容的方法:

 String [] options = {"A选项","B选项","C选项","D选项"};String info =  (String)JOptionPane.showInputDialog(null,"请输入你的选项:","提示",JOptionPane.QUESTION_MESSAGE,null,options,options[2]);System.out.println(info);  

消息对话框

消息对话框(showMessageDialog)有三种构造函数,具体如下:

 1、JOptionPane.showMessageDialog(parentComponent, message);2、JOptionPane.showMessageDialog(parentComponent, message, title, messageType);3、JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon);  

实例如下:

 JOptionPane.showMessageDialog(null,"这里是消息提示对话框!","消息提示",JOptionPane.WARNING_MESSAGE);//消息对话框  

效果如下:

超详细的Java弹窗样式及使用教程

选择对话框

选择对话框(howOptionDialog)只有一种构造函数如下:

  JOptionPane.showOptionDialog(parentComponent, message, title, optionType, messageType, icon, options, initialValue)  

使用实例如下。最后一个参数表示默认选择的内容,:

 String [] options = {"A选项","B选项","C选项","D选项"};JOptionPane.showOptionDialog(null,"请选择你的选项:","提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);//选择对话框*/  

效果如下:

超详细的Java弹窗样式及使用教程

选择对话框下获取用户选项的方法:

 String [] options = {"A选项","B选项","C选项","D选项"};int n =  JOptionPane.showOptionDialog(null,"请选择你的选项:","提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0]);//选择对话框*/System.out.println(options[n]);  

自定义消息图标

自定义对话框图标的方法如下:

 ImageIcon icon = new ImageIcon("it.jpg");//注意设置图片尺寸,50*50px较适合JOptionPane.showMessageDialog(null, "这是自定义图标!","提示",JOptionPane.WARNING_MESSAGE,icon);//该消息框的提示图标会被自定义的图标覆盖掉  

效果如下:

超详细的Java弹窗样式及使用教程

关于JOptionPane类中弹窗的使用就先分享到这里,之后还会对其他使用继续更新!

觉得有用记得点赞关注哟!

文章来源:智云一二三科技

文章标题:超详细的Java弹窗样式及使用教程

文章地址:https://www.zhihuclub.com/184189.shtml

关于作者: 智云科技

热门文章

网站地图