我有一个框架,当用户想要删除记录时,应该显示一个警告窗格。
但是,现在我必须认识到,如果用户选择是,然后我删除选定的行,如果选择否,不要删除它!
如何?
if (e.getSource() == deleteUser) {
JOptionPane.showConfirmDialog(rootPane, "Are You Sure To Delete?", "Delete User", WIDTH);
// if yes, Then remove
}
我有一个框架,当用户想要删除记录时,应该显示一个警告窗格。
但是,现在我必须认识到,如果用户选择是,然后我删除选定的行,如果选择否,不要删除它!
如何?
if (e.getSource() == deleteUser) {
JOptionPane.showConfirmDialog(rootPane, "Are You Sure To Delete?", "Delete User", WIDTH);
// if yes, Then remove
}
从 JavaDocs...
public static int showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType)
throws HeadlessException
Brings up a dialog where the number of choices is determined by the optionType parameter.
Parameters:
parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used
message - the Object to display
title - the title string for the dialog
optionType - an int designating the options available on the dialog: YES_NO_OPTION, YES_NO_CANCEL_OPTION, or OK_CANCEL_OPTION
Returns:
an int indicating the option selected by the user
返回类型将取决于您传递给optionType
参数的值
这建议你应该做类似的事情......
int result = JOptionPane.showConfirmDialog(rootPane, "Are You Sure To Delete?", "Delete User", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
// Do something here
}
查看如何制作对话框以获取更多详细信息...