作swing程序中遇到使用消息提示框的,JOptionPane類其中封裝了不少的方法。函數
很方便的,因而就簡單的整理了一下。this
1.1 showMessageDialogspa
顯示一個帶有OK 按鈕的模態對話框。3d
下面是幾個使用showMessageDialog 的例子:blog
Java代碼 it
- JOptionPane.showMessageDialog(null, "友情提示");
- 例子:JOptionPane.showMessageDialog(Login.this,"用戶名與密碼沒法登陸", "登陸失敗",JOptionPane.ERROR_MESSAGE)
效果以下:io
![JOptionPane類提示框的一些經常使用的方法](http://static.javashuo.com/static/loading.gif)
Java代碼 登錄
- JOptionPane.showMessageDialog(jPanel, "提示消息", "標題",JOptionPane.WARNING_MESSAGE);
效果以下:
程序
Java代碼 密碼
- JOptionPane.showMessageDialog(null, "提示消息.", "標題",JOptionPane.ERROR_MESSAGE);
![JOptionPane類提示框的一些經常使用的方法](http://static.javashuo.com/static/loading.gif)
Java代碼
- JOptionPane.showMessageDialog(null, "提示消息.", "標題",JOptionPane.PLAIN_MESSAGE);
![JOptionPane類提示框的一些經常使用的方法](http://static.javashuo.com/static/loading.gif)
1.2 showOptionDialog
這個函數能夠改變顯示在按鈕上的文字。你還能夠執行更多的個性化操做。
常規的消息框:
Java代碼
- int n = JOptionPane.showConfirmDialog(null, "你高興嗎?", "標題",JOptionPane.YES_NO_OPTION);//i=0/1
效果以下:
![JOptionPane類提示框的一些經常使用的方法](http://static.javashuo.com/static/loading.gif)
個性話消息框:
Java代碼
- Object[] options ={ "好啊!", "去一邊!" };
- int m = JOptionPane.showOptionDialog(null, "我能夠約你嗎?", "標題",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
效果以下:
![JOptionPane類提示框的一些經常使用的方法](http://static.javashuo.com/static/loading.gif)
1.3 showInoutDialog
該方法返回一個Object 類型。這個Object 類型通常是一個String 類型,反應了用戶的輸入。
下拉列表形式的例子:
Java代碼
- Object[] obj2 ={ "足球", "籃球", "乒乓球" };
- String s = (String) JOptionPane.showInputDialog(null,"請選擇你的愛好:\n", "愛好", JOptionPane.PLAIN_MESSAGE, new ImageIcon("icon.png"), obj2, "足球");
效果以下:
![JOptionPane類提示框的一些經常使用的方法](http://static.javashuo.com/static/loading.gif)
文本框形式的例子:
Java代碼
- JOptionPane.showInputDialog(null,"請輸入你的愛好:\n","title",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");
效果以下:
![JOptionPane類提示框的一些經常使用的方法](http://static.javashuo.com/static/loading.gif)
對應的小圖標可參照下圖:
![JOptionPane類提示框的一些經常使用的方法](http://static.javashuo.com/static/loading.gif)