javax.swing.JOptionPane 类的 showConfirmDialog 方法

来源:互联网 发布:天翼进销存软件汽配 编辑:程序博客网 时间:2024/06/06 21:05

/在编写大型实验过程中,我需要完成对用户界面的保存,打开,新建和退出操作。其中会有多次操作提醒,使用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);

阅读全文
0 0
原创粉丝点击