Java JOptionPane2024年10月24日 | 阅读 4 分钟 JOptionPane 类用于提供标准对话框,例如消息对话框、确认对话框和输入对话框。 这些对话框用于显示信息或从用户获取输入。 JOptionPane 类继承自 JComponent 类。 JOptionPane 类声明JOptionPane 类的常用构造函数构造函数 | 描述 |
---|
JOptionPane() | 用于创建带有测试消息的 JOptionPane。 | JOptionPane(Object message) | 用于创建 JOptionPane 的实例以显示消息。 | JOptionPane(Object message, int messageType | 用于创建 JOptionPane 的实例,以显示具有指定消息类型和默认选项的消息。 | JOptionPane(Object message, int messageType, int optionType)) | 构造一个新的 JOptionPane,以显示具有指定消息类型和选项类型的指定消息。 | JOptionPane(Object message, int messageType, int optionType, Icon icon) | 构造一个新的 JOptionPane,以显示具有指定消息类型、选项类型和图标的指定消息。 |
JOptionPane 类的常用方法方法 | 描述 |
---|
JDialog createDialog(String title) | 用于创建并返回具有指定标题的新无父级 JDialog。 | static void showMessageDialog(Component parentComponent, Object message) | 用于创建一个标题为“消息”的信息消息对话框。 | static void showMessageDialog(Component parentComponent, Object message, String title, int messageType) | 用于创建具有给定标题和消息类型的消息对话框。 | static int showConfirmDialog(Component parentComponent, Object message) | 用于创建一个包含选项“是”、“否”和“取消”的对话框; 标题为“选择一个选项”。 | static String showInputDialog(Component parentComponent, Object message) | 用于显示一个问题消息对话框,请求用户输入,父组件为 parentComponent。 | void setInputValue(Object newValue) | 用于设置用户选择或输入的输入值。 | void setMessage(Object newMessage) | 设置此对话框中显示的消息。 | void setMessage(Object newMessage, int messageType) | 设置消息类型和此对话框中显示的消息。 | void setOptions(Object[] newOptions) | 设置要在对话框窗口中显示的选项。 | void setOptionType(int newType) | 设置对话框中显示的选项类型。 | void setInitialValue(Object newValue) | 设置用户选择或输入的初始值。 | int showOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) | 向用户呈现一个自定义对话框。 | static void showInternalMessageDialog(Component parentComponent, Object message) | 创建一个具有指定消息的内部对话框。 | static void showInternalMessageDialog(Component parentComponent, Object message, String title, int messageType) | 创建一个具有指定标题和消息类型的内部消息对话框。 | static int showInternalConfirmDialog(Component parentComponent, Object message) | 创建一个包含选项“是”、“否”和“取消”的内部对话框,标题为“选择一个选项”。 | static String showInternalInputDialog(Component parentComponent, Object message) | 显示一个内部问题消息对话框,请求用户输入。 | static void showInternalOptionDialog(Component parentComponent, Object message, String title, int optionType, int messageType, Icon icon, Object[] options, Object initialValue) | 向用户呈现一个自定义的内部对话框。 |
Java JOptionPane 示例:showMessageDialog()文件名:OptionPaneExample.java 输出  说明 此Java程序演示了如何使用 JOptionPane 类创建一个简单的图形消息对话框。它导入 javax.swing 包并定义一个名为 OptionPaneExample 的类。在类内部,它初始化一个 JFrame 对象 f。在 OptionPaneExample 的构造函数中,使用 JOptionPane.showMessageDialog() 创建一个消息对话框,显示消息“Hello, Welcome to Javatpoint.” JFrame 对象 f 作为对话框的父组件传递。最后,main 方法实例化 OptionPaneExample 类,从而在执行时触发消息对话框的显示。 Java JOptionPane 示例:showMessageDialog()文件名:OptionPaneExample.java 输出  说明 此 Java 应用程序演示了如何使用 JOptionPane 类构造一个带有警告图标和自定义标题的消息对话框。 OptionPaneExample 定义为一个类,并导入 javax.swing 包。在类中初始化一个名为 f 的 JFrame 对象。 JOptionPane.showMessageDialog() 在 OptionPaneExample 构造函数中用于实例化消息对话框。它显示自定义标题“Alert”和警告图标 (JOptionPane.WARNING_MESSAGE) 以及消息“Successfully Updated”。对话框的父组件是 JFrame 对象 f。 main() 方法通过创建 OptionPaneExample 的实例结束,该实例会导致自定义消息对话框在执行时出现。 Java JOptionPane 示例:showInputDialog()文件名:OptionPaneExample.java 输出  Java JOptionPane 示例:showConfirmDialog()文件名:OptionPaneExample.java 输出  说明 此 Java 应用程序展示了如何利用事件处理和 JOptionPane 类在关闭 JFrame 窗口之前征求用户的批准。 java.awt.event 和 javax.swing 包都被导入。通过扩展 WindowAdapter,OptionPaneExample 类覆盖了 windowClosing 函数。在构造函数中初始化并配置一个名为 f 的 JFrame 对象。 f.addWindowListener(this) 将 WindowAdapter 的实例作为窗口侦听器添加到 f。 JOptionPane.showConfirmDialog() 用于覆盖 windowClosing 函数并显示一个确认对话框,询问用户“确定吗?” 单击“是”会导致应用程序将框架的默认关闭操作更改为 JFrame.EXIT_ON_CLOSE,从而有效地关闭窗口。最后,main() 方法实例化 OptionPaneExample,它还设置事件处理并创建 JFrame 窗口。
|