0

ユーザーがレコードを削除したいときに、警告ペインを表示するフレームがあります。

しかし、ユーザーが「はい」を選択した場合、選択した行を削除し、「いいえ」を選択した場合は削除しないでください!

どのように?

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

// if yes, Then remove
}
4

1 に答える 1

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
}

詳細については、ダイアログの作成方法をご覧ください...

于 2013-05-10T07:40:47.990 に答える