Java的showConfirmDialog

showConfirmDialog 是 Java Swing 库中 JOptionPane 类的一个方法,用于显示一个模态对话框,其中包含一些选项按钮(如是、否、取消),以及一个可选的消息或复杂的组件集。用户的响应可以用于在程序中做出决策。

以下是 showConfirmDialog 方法的一些常用变体:

  1. showConfirmDialog(Component parentComponent, Object message)
  2. showConfirmDialog(Component parentComponent, Object message, String title, int optionType)
  3. showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType)
  4. showConfirmDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon)

其中参数的含义如下:

  • parentComponent: 对话框的父组件,它影响对话框的显示位置。如果传递 null,对话框将出现在屏幕中央。
  • message: 要显示的 Object,可以是字符串、图标或任何复杂的组件。
  • title: 对话框的标题。
  • optionType: 指定显示哪些按钮。可能的值有 JOptionPane.YES_NO_OPTIONJOptionPane.YES_NO_CANCEL_OPTION, 和 JOptionPane.OK_CANCEL_OPTION
  • messageType: 指定显示哪种消息类型的图标。可能的值有 JOptionPane.ERROR_MESSAGEJOptionPane.INFORMATION_MESSAGEJOptionPane.WARNING_MESSAGEJOptionPane.QUESTION_MESSAGE, 或 JOptionPane.PLAIN_MESSAGE
  • icon: 自定义图标,用于替换默认图标。

showConfirmDialog 方法会返回一个整数,指示用户按下了哪个按钮。这些返回值对应于 JOptionPane 的静态字段:YES_OPTIONNO_OPTIONCANCEL_OPTIONOK_OPTION, 和 CLOSED_OPTION

下面是一个简单的示例,展示了如何使用 showConfirmDialog 方法:

import javax.swing.*;

public class ConfirmDialogExample {
    public static void main(String[] args) {
        // 在 Swing 应用程序中,应当在事件分发线程中执行 GUI 操作。
        SwingUtilities.invokeLater(() -> {
            int response = JOptionPane.showConfirmDialog(null, "Do you want to save changes?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);

            switch (response) {
                case JOptionPane.YES_OPTION:
                    System.out.println("User chose Yes.");
                    // 保存更改
                    break;
                case JOptionPane.NO_OPTION:
                    System.out.println("User chose No.");
                    // 不保存更改
                    break;
                case JOptionPane.CANCEL_OPTION:
                    System.out.println("User chose to cancel.");
                    // 取消操作
                    break;
                case JOptionPane.CLOSED_OPTION:
                    System.out.println("User closed the dialog.");
                    // 对话框被关闭
                    break;
                default:
                    // 其他情况
                    break;
            }
        });
    }
}

在这个例子中,当程序运行时,会显示一个确认对话框,询问用户是否要保存更改。用户的选择将通过控制台输出来显示,并可以根据返回的整数值来执行相应的操作。

下面是几个 JOptionPane.showConfirmDialog 方法的不同使用场景的例子。

例1:基础确认对话框

import javax.swing.*;

public class ConfirmDialogExample {
    public static void main(String[] args) {
        // 使用基础的确认对话框,只有确认和取消选项
        int result = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete the file?", "Confirmation", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            // 用户选择了确认
            System.out.println("File deleted");
        } else {
            // 用户选择了取消或者关闭了对话框
            System.out.println("File deletion cancelled");
        }
    }
}

例2:带有多个选项的确认对话框(是/否/取消)

import javax.swing.*;

public class ConfirmDialogExample {
    public static void main(String[] args) {
        // 显示一个有是、否和取消选项的确认对话框
        int result = JOptionPane.showConfirmDialog(null, "Would you like to save your progress?", "Save Progress", JOptionPane.YES_NO_CANCEL_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            // 用户选择了是
            System.out.println("Progress saved");
        } else if (result == JOptionPane.NO_OPTION) {
            // 用户选择了否
            System.out.println("Progress not saved");
        } else {
            // 用户选择了取消或者关闭了对话框
            System.out.println("Cancelled");
        }
    }
}

例3:带有自定义标题和自定义图标的确认对话框

import javax.swing.*;

public class ConfirmDialogExample {
    public static void main(String[] args) {
        ImageIcon icon = new ImageIcon("path/to/your/icon.png"); // 使用自定义图标
        // 显示带有自定义标题和图标的确认对话框
        int result = JOptionPane.showConfirmDialog(null, "Do you agree with the terms and conditions?", "Agreement", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, icon);
        if (result == JOptionPane.YES_OPTION) {
            // 用户同意了条款
            System.out.println("Terms and conditions accepted");
        } else {
            // 用户不同意条款
            System.out.println("Terms and conditions declined");
        }
    }
}

例4:在特定的父组件中显示确认对话框

假设你有一个 JFrame 或其他类型的组件,你想要对话框相对于这个组件来显示。

import javax.swing.*;

public class ConfirmDialogExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        // 初始化 JFrame 的其他设置
        // ...

        frame.setVisible(true);

        // 显示对话框,它相对于我们的 JFrame 位置居中
        int result = JOptionPane.showConfirmDialog(frame, "Proceed with action?", "Confirm Action", JOptionPane.YES_NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {
            // 用户选择了继续
            System.out.println("Action proceeded");
        } else {
            // 用户选择了不继续
            System.out.println("Action cancelled");
        }
    }
}

例5:使用 Lambda 表达式在事件分发线程中显示确认对话框

import javax.swing.*;

public class ConfirmDialogExample {
    public static void main(String[] args) {
        // 在事件分发线程中执行 GUI 操作
        SwingUtilities.invokeLater(() -> {
            int result = JOptionPane.showConfirmDialog(null, "Exit the application?", "Exit", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                System.exit(0); // 退出应用
            }
        });
    }
}

在这些示例中,请确保已经导入了 Swing 库,并且如果你使用的是图标,那么图标的路径是正确的。每个示例中的对话框都可以根据实际应用程序的需求进行调整。

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值