识别用户在Joptionpane中选择是或否

时间:2013-05-10 07:35:39

标签: java swing joptionpane

我有一个框架,当用户想要删除记录时,应显示警告窗格。

但是,现在我必须认识到,如果用户选择是,那么我删除所选行,如果选择否,请不要删除它!

如何?

if (e.getSource() == deleteUser) {
JOptionPane.showConfirmDialog(rootPane, "Are You Sure To Delete?", "Delete User", WIDTH);

// if yes, Then remove
}

1 个答案:

答案 0 :(得分:4)

来自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
}

查看How to make dialogs了解更多详情......