今天来和大家分享一个Swing程序设计中关于JOptionPane类的使用,该类的作用呢,其实主要就是设置弹窗,所以在这里也就和大家总结了常用的弹窗设置的方法以及JOptionPane类详细使用说明! JOptionPane类属于Swing组件中的一种,所以导入方式如下: import javax.swing.JOptionPane; 四种消息提示框 在该类中常用的常用的四种消息提示框为: showConfirmDialog ,"提示",JOptionPane.OK_OPTION,JOptionPane.QUESTION_MESSAGE); //确认对话框 效果如下: ? ,"提示",JOptionPane.OK_OPTION,JOptionPane.QUESTION_MESSAGE); //确认对话框 //如果用户选择的是OK if (userOption == JOptionPane.OK_OPTION (null,"请选择你的选项:","提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[0])
password1.equals(password2)){ JOptionPane.showMessageDialog(null,"两个密码填写不一致"); ; }else{ JOptionPane.showMessageDialog(null,"注册失败!!") ; jButton2ActionPerformed(evt); }else{ JOptionPane.showMessageDialog (null,"请选择要删除的记录"); return; } int n=JOptionPane.showConfirmDialog(null,"Are (null,"请选择要删除的记录"); return; } int n=JOptionPane.showConfirmDialog(null,"Are
password1.equals(password2)){ JOptionPane.showMessageDialog(null,"两个密码填写不一致"); ; }else{ JOptionPane.showMessageDialog(null,"注册失败!!") ; jButton2ActionPerformed(evt); }else{ JOptionPane.showMessageDialog (null,"请选择要删除的记录"); return; } int n=JOptionPane.showConfirmDialog(null,"Are (null,"请选择要删除的记录"); return; } int n=JOptionPane.showConfirmDialog(null,"Are
; JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-提示信息", JOptionPane.INFORMATION_MESSAGE ; JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-提示信息", JOptionPane.INFORMATION_MESSAGE ; JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-提示信息", JOptionPane.INFORMATION_MESSAGE ; JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-提示信息", JOptionPane.INFORMATION_MESSAGE ; JOptionPane.showConfirmDialog(Split.this, label_result,"文件切割-打开目录", JOptionPane.INFORMATION_MESSAGE
, "表白幺~",JOptionPane.PLAIN_MESSAGE); } }); button.setBounds(98, 179, 93 (null, "房子写你名", "表白~",JOptionPane.YES_NO_OPTION); JOptionPane.showConfirmDialog(null, "我会做饭", "表白~",JOptionPane.YES_NO_OPTION); JOptionPane.showConfirmDialog(null, "我会洗衣服 ", "表白~",JOptionPane.YES_NO_OPTION); JOptionPane.showMessageDialog(null, "现在可以成为我女票吗? , "表白幺~",JOptionPane.PLAIN_MESSAGE); JOptionPane.showMessageDialog(null, "恭喜!成功拿下!"
%n",name); String age=sc.next(); System.out.println(age); 2、JOptionPane 图形化 JOptionPane 图形化,点击 确定 输出框内容,点击其他返回 null //2、JOptionPane 图形化,点击确定输出框内容,点击其他返回null String w= JOptionPane.showInputDialog (s); String s1 = JOptionPane.showInputDialog(null, "请输入词汇", "超级词典", JOptionPane.QUESTION_MESSAGE); System.out.println (s1);//问号图标 String s2 = JOptionPane.showInputDialog(null, "请输入词汇", "超级词典", JOptionPane.ERROR_MESSAGE) ); System.out.println(s3);//警告⚠图标 String s4 = JOptionPane.showInputDialog(null, "内容", "标题", JOptionPane.OK_CANCEL_OPTION
moreRecords) { JOptionPane.showMessageDialog(null, "结果无记录", "无记录", JOptionPane.INFORMATION_MESSAGE) ()) { JOptionPane.showMessageDialog(null, "请输入密码", "提示消息", JOptionPane.WARNING_MESSAGE); this.clear , "提示消息", JOptionPane.WARNING_MESSAGE); } else if (jtf.getText().isEmpty()) { JOptionPane.showMessageDialog , "提示消息", JOptionPane.WARNING_MESSAGE); } else { JOptionPane.showMessageDialog(null, "用户名或者密码错误! , "提示消息", JOptionPane.WARNING_MESSAGE); } else { JOptionPane.showMessageDialog(null, "用户名或者密码错误!
"); return; } if(IfBookIdExit(id)){ JOptionPane.showMessageDialog(null,"图书编号已存在"); return ; clearAllText(); } }catch(ParseException e2){ JOptionPane.showMessageDialog(null,"出版时间格式错误 ,正确为(年-月)"); }catch(NumberFormatException e1){ JOptionPane.showMessageDialog(null,"库存数量,价格,页数错误, (null,"读者添加成功"); }else{ JOptionPane.showMessageDialog(null,"读者添加失败"); } DbOp.close(); (null,"yet It's Successed Update PassWord"); }else{ JOptionPane.showMessageDialog(null,"Sorroy
"); return; } if(IfBookIdExit(id)){ JOptionPane.showMessageDialog(null,"图书编号已存在"); return ; clearAllText(); } }catch(ParseException e2){ JOptionPane.showMessageDialog(null,"出版时间格式错误 ,正确为(年-月)"); }catch(NumberFormatException e1){ JOptionPane.showMessageDialog(null,"库存数量,价格,页数错误, (null,"读者添加成功"); }else{ JOptionPane.showMessageDialog(null,"读者添加失败"); } DbOp.close(); (null,"yet It's Successed Update PassWord"); }else{ JOptionPane.showMessageDialog(null,"Sorroy
; JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.INFORMATION_MESSAGE ; JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.INFORMATION_MESSAGE ; JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.WARNING_MESSAGE ; JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.INFORMATION_MESSAGE ; JOptionPane.showConfirmDialog(tabbedPane, label_result,"图书管理系统-密码修改", JOptionPane.WARNING_MESSAGE
JOptionPane 提供了如下 4 个方法来创建对话框 。 (jf, jta.getText(), "消息对话框", JOptionPane.ERROR_MESSAGE); //JOptionPane.showMessageDialog( (jf, jta.getText(), "消息对话框", JOptionPane.WARNING_MESSAGE); //JOptionPane.showMessageDialog (jf, jta.getText(), "消息对话框", JOptionPane.PLAIN_MESSAGE); JOptionPane.showMessageDialog(jf (jf, jta.getText(), "确认对话框",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if
import javax.swing.JOptionPane; import java.text.DecimalFormat; public class url { public static tstr5,result; int U,again; double t1,t2,t3,t4,t5,Q1,Q2,Q3,Q4,Q5,Q,n,e,N; do { numStr=JOptionPane.showInputDialog (“请输入电压: “); U=Integer.parseInt(numStr); //第一个时间 tstr1=JOptionPane.showInputDialog n+”\n\n[n]的值为:\n[n]==”+fmt.format(n)+”\n\n基本电量e:\ne==Q/[n]==”+Q/Integer.parseInt(fmt.format(n)); JOptionPane.showMessageDialog (null,result); again=JOptionPane.showConfirmDialog(null,”Do Another”); } while (again==JOptionPane.YES_OPTION
; public class TestUi2 { public static void main(String[] args) { //信息弹框 JOptionPane.showMessageDialog (null, "欢迎光临"); //消息确认框 int i = JOptionPane.showConfirmDialog(null, "早上好"); if(i==0) { JOptionPane.showMessageDialog (null, "早上好呀"); }else { JOptionPane.showMessageDialog(null, "不好"); } //输入框 String str = JOptionPane.showInputDialog (null,"请输入你的姓名:"); JOptionPane.showMessageDialog(null, str); String age = JOptionPane.showInputDialog (null, "Do you love me"); if(n==0) { JOptionPane.showMessageDialog(null, "Me too"); break;
,"提示:",JOptionPane.PLAIN_MESSAGE); system system = new system(); frame.dispose(); } else { JOptionPane.showMessageDialog(frame, "账号或者密码错误!" ,"请确认",JOptionPane.YES_NO_OPTION); if(result==JOptionPane.OK_OPTION) { String sql = "delete ,"提示:",JOptionPane.ERROR_MESSAGE); } } //------------------------------保存--------------------- "请确认数值已经更改,否则保存无效","请确认",JOptionPane.YES_NO_OPTION); if(result==JOptionPane.OK_OPTION) { int
; JOptionPane.showConfirmDialog(Merge.this, label_result,"文件合并-打开目录", JOptionPane.INFORMATION_MESSAGE ,JOptionPane.OK_OPTION, new ImageIcon(Merge.class.getResource("/resources/null.png"))); } } ; JOptionPane.showConfirmDialog(Merge.this, label_result,"文件合并-错误提示", JOptionPane.INFORMATION_MESSAGE ; JOptionPane.showConfirmDialog(Merge.this, label_result,"文件合并-错误提示", JOptionPane.INFORMATION_MESSAGE ; JOptionPane.showConfirmDialog(Merge.this, label_result,"文件合并-成功提示", JOptionPane.INFORMATION_MESSAGE
1.显示输入消息框,可以输入数据 String str1 = JOptionPane.showInputDialog(“输入消息框”,“0”); 2.显示出一个弹框 null表示对话框显示在屏幕中间 第二个参数表示要显示的字符结果 JOptionPane.showMessageDialog(null,str1); JOptionPane.showMessageDialog(null,“a + ("输入运算符号","+"); String str3 = JOptionPane.showInputDialog("输入b的值","0"); b = Integer.parseInt (null, "三个数的最大值为:" + max); JOptionPane.showMessageDialog(null, "三个数的最小值为:" + min); JOptionPane.showMessageDialog (null, "三个数的平均值为:" + ave); JOptionPane.showMessageDialog(null, "三个数由大到小为:" + Arrays.toString
, "提示", JOptionPane.YES_NO_OPTION); if (option == JOptionPane.YES_OPTION , "提示", JOptionPane.YES_NO_OPTION); if (option == JOptionPane.YES_OPTION , "提示", JOptionPane.YES_NO_OPTION); if (option == JOptionPane.YES_OPTION , "提示", JOptionPane.YES_NO_OPTION); if (option == JOptionPane.YES_OPTION , "提示", JOptionPane.YES_NO_OPTION); if (option == JOptionPane.YES_OPTION
, "提示", JOptionPane.ERROR_MESSAGE); } //如果输入了正确格式的端口号则继续,否则不执行 if (isCorrectPort) { , "提示", JOptionPane.ERROR_MESSAGE); } //JOptionPane.showMessageDialog(null, "服务器端已断开!" , "提示", JOptionPane.ERROR_MESSAGE); } } 客户端 在客户端我们同样需要按照和服务器端一样的思路进行编写, 客户端界面设计 首先是界面的设计,效果如下 JOptionPane.showMessageDialog(null, "输入完成,正在连接...... JOptionPane.showMessageDialog(null, "客户器端已断开!"
; JOptionPane.showConfirmDialog(AddUserFrame.this, label_result,"图书管理系统-添加用户", JOptionPane.WARNING_MESSAGE ,JOptionPane.OK_OPTION , new ImageIcon("result.png")); } else if(reiteration==true)//如果重复了 ; JOptionPane.showConfirmDialog(AddUserFrame.this, label_result,"图书管理系统-添加用户", JOptionPane.WARNING_MESSAGE ; JOptionPane.showConfirmDialog(AddUserFrame.this, label_result,"图书管理系统-添加用户", JOptionPane.INFORMATION_MESSAGE ; JOptionPane.showConfirmDialog(AddUserFrame.this, label_result,"图书管理系统-添加用户", JOptionPane.WARNING_MESSAGE
1.显示输入消息框,可以输入数据 String str1 = JOptionPane.showInputDialog(“输入消息框”,“0”); 2.显示出一个弹框 null表示对话框显示在屏幕中间 第二个参数表示要显示的字符结果 JOptionPane.showMessageDialog(null,str1); JOptionPane.showMessageDialog(null,“a + ("输入运算符号","+"); String str3 = JOptionPane.showInputDialog("输入b的值","0"); b = Integer.parseInt (null, "三个数的最大值为:" + max); JOptionPane.showMessageDialog(null, "三个数的最小值为:" + min); JOptionPane.showMessageDialog (null, "三个数的平均值为:" + ave); JOptionPane.showMessageDialog(null, "三个数由大到小为:" + Arrays.toString