javax.swing.JOptionPane 類的 showConfirmDialog 方法


/*在編寫大型實驗過程中,我需要完成對用戶界面的保存,打開,新建和退出操作。其中會有多次操作提醒,使用showConfirmDialog可以直接顯示提示框,不用自己在新建

 

showConfirmDialog方法

//************************************************************************
// 新建、保存、打開、退出
//************************************************************************
public void New_file() {
if (ta.getText().length() == 0) {
ta.setText(null);
} else {
int valuex = JOptionPane.showConfirmDialog(null,"是否保存當前修改?", "請確認",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (valuex == 0) {
Save_file();
} else if (valuex == 1) {
ta.setText(null);
}
}
}
public void Open_file() {
openDia = new FileDialog(frm, "打開", FileDialog.LOAD);
if (ta.getText().length() == 0) {
openDia.setVisible(true);// 顯示打開文件對話框
String dirpath = openDia.getDirectory();// 獲取打開文件路徑並保存到字符串中。
String fileName = openDia.getFile();// 獲取打開文件名稱並保存到字符串中
if (dirpath == null || fileName == null)// 判斷路徑和文件是否為空
return;
else// 文件不為空
{
ta.setText(null);
}
file = new File(dirpath, fileName);// 創建新的路徑和名稱
try {
BufferedReader bufr = new BufferedReader(new FileReader(file));// 嘗試從文件中讀東西
String line = null;// 變量字符串初始化為空
while ((line = bufr.readLine()) != null) {
ta.append(line + "\r\n");// 顯示每一行內容
}
bufr.close();// 關閉文件
} catch (FileNotFoundException e1) {
// 拋出文件路徑找不到異常
e1.printStackTrace();
} catch (IOException e1) {
// 拋出IO異常
e1.printStackTrace();
}
}
else {
int valuex = JOptionPane.showConfirmDialog(null,"是否保存當前文件?", "請確認",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (valuex == 0) {
Save_file();
} else if (valuex == 1) {
ta.setText(null);
Open_file();
}
}
}
public void Save_file() {
saveDia = new FileDialog(frm, "保存", FileDialog.SAVE);
if (file == null) {
saveDia.setVisible(true);// 顯示保存文件對話框
String dirpath = saveDia.getDirectory();// 獲取保存文件路徑並保存到字符串中。
String fileName = saveDia.getFile();// //獲取打保存文件名稱並保存到字符串中
if (dirpath == null || fileName == null)// 判斷路徑和文件是否為空
return;// 空操作
else
file = new File(dirpath, fileName);// 文件不為空,新建一個路徑和名稱
}
try {
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String text = ta.getText();// 獲取文本內容
bufw.write(text);// 將獲取文本內容寫入到字符輸出流
bufw.close();// 關閉文件
} catch (IOException e1) {
// 拋出IO異常
e1.printStackTrace();
}
}
public void Quit_file() {
int valuex = JOptionPane.showConfirmDialog(null,"你確認要退出程序嗎?", "請確認",JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (valuex == 0) {
System.exit(0);
}
}

 

//***********************************************************

//       showConfirmDialog相關

//***********************************************************

showConfirmDialog共有4個重載方法。

1) showConfirmDialog(Component parentComponent, Object message);

message為提示信息

默認的有三個按鈕,“是”,“否”,“取消”。

2)showConfirmDialog(Component parentComponent, Object message, String title, int optionType);

title為對話框標題

optionType為按鈕的設置

DEFAULT_OPTION //“確定”按鈕   只有一個“確定”按鈕,返回值為0

YES_NO_OPTION // “是”、“否”按鈕    “是”返回值0,“否”返回值1

YES_NO_CANCEL_OPTION //“是”、“否”、“取消”按鈕   “是”返回值0,“否”返回值1, “取消”返回值2

OK_CANCEL_OPTION //“確定”、“取消”按鈕   “確定”返回值0,“取消”返回值2

3)showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType);

“ messageType”為圖標類型的參數,具體取值是:

ERROR_MESSAGE

INFORMATION_MESSAGE

WARNING_MESSAGE

QUESTION_MESSAGE(默認類型)

PLAIN_MESSAGE(無圖標)

4)showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon);

增加參數“Icon icon”,通過該參數,用戶可以把自己的圖標添加到對話框中;

Icon icon=new ImageIcon("grapes.gif");

Int n=JOptionPane.showConfirmDialog(null,"提示信息","標題",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane. INFORMATION_MESSAGE,icon);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM