语雀笔记:https://www.yuque.com/huangzhanqi/rhwoir/paaoghdyv0tgksk1
https://www.yuque.com/huangzhanqi/rhwoir/paaoghdyv0tgksk1Java图形化界面: Java图形化界面学习demo与资料 (gitee.com)
https://gitee.com/zhanqi214/java-graphical-interface
通过 JOptionPane 可以非常方便地创建一些简单的对话框, Swing 已经为这些对话框添加了相应的组件,无须程序员手动添加组件 。 JOptionPane 提供了如下 4 个方法来创建对话框 。
| 方法名称 | 方法功能 |
| showMessageDialog/showInternalMessageDialog | 消息对话框 ,告知用户某事己发生 , 用户只能单击"确定"按钮 , 类似于 JavaScript 的 alert 函数 。 |
| showConfirmDialog/showInternalConfirmDialog | 确认对话框,向用户确认某个问题,用户可以选择 yes 、 no ~ cancel 等选项 。 类似于 JavaScript 的 comfirm 函数 。该方法返回用户单击了 哪个按钮 |
| showInputDialog/showInternalInputDialog | 输入对话框,提示要求输入某些信息,类似于 JavaScript的 prompt 函数。该方法返回用户输入的字符串 。 |
| showOptionDialog/showInternalOptionDialog | 自定义选项对话框 ,允许使用自 定义选项 ,可以取代showConfirmDialog 所产生的对话框,只是用起来更复杂 。 |
上述方法都有都有很多重载形式,选择其中一种最全的形式,参数解释如下:
- --参数解释:
- parentComponent:当前对话框的父组件
- message:对话框上显示的信息,信息可以是字符串、组件、图片等
- title:当前对话框的标题
- optionType:当前对话框上显示的按钮类型:DEFAULT_OPTION、YES_NO_OPTION、YES_NO_CANCEL_OPTION、OK_CANCEL_OPTION
- messageType:当前对话框的类型:ERROR_MESSAGE、INFORMATION_MESSAGE、WARNING_MESSAGE、QUESTION_MESSAGE、PLAIN_MESSAGE
- icon:当前对话框左上角的图标
- options:自定义下拉列表的选项
- initialValue:自定义选项中的默认选中项
-
- showXxxDialog(Component parentComponent,
- Object message,
- String title,
- int optionType,
- int messageType,
- Icon icon,
- Object[] options,
- Object initialValue)
当用户与对话框交互结束后,不同类型对话框的返回值如下:
对 showConfirmDialog 所产生的对话框,有如下几个返回值:

- package swing.day01;
-
- import awt.day01.Utils;
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
-
- public class MessageDialogTest {
-
- JFrame frame = new JFrame("消息对话框");
- JTextArea textArea = new JTextArea(8,20);
- JButton button=new JButton(new AbstractAction("弹出消息对话框") {
- @Override
- public void actionPerformed(ActionEvent e) {
- JOptionPane.showMessageDialog(frame,textArea.getText(),"消息对话框",JOptionPane.YES_OPTION,new ImageIcon("src/swing/img/component/female.png"));
- textArea.append("用户点击了确定按钮");
- }
- });
-
- public void init(){
- frame.add(textArea);
- textArea.setText("Hello China");
- frame.add(button, BorderLayout.SOUTH);
-
- Utils.setJFrame(frame);
- }
-
- public static void main(String[] args) {
- new MessageDialogTest().init();
- }
- }
确认对话框:

- package swing.day01;
-
- import awt.day01.Utils;
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
-
- public class ConfirmDialogTest {
- JFrame frame = new JFrame("确认对话框");
- JTextArea textArea = new JTextArea(8,20);
- JButton button=new JButton(new AbstractAction("确认对话框") {
- @Override
- public void actionPerformed(ActionEvent e) {
- int i = JOptionPane.showConfirmDialog(frame, textArea.getText(), "消息对话框", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, new ImageIcon("src/swing/img/component/female.png"));
- if (i==1) {
- textArea.append("用户点击了确认按钮\n");
- }else {
- textArea.append("用户点击了取消按钮\n");
- }
- }
- });
-
- public void init(){
- frame.add(textArea);
- textArea.setText("Hello China\n");
- frame.add(button, BorderLayout.SOUTH);
-
- Utils.setJFrame(frame);
- }
-
- public static void main(String[] args) {
- new ConfirmDialogTest().init();
- }
- }


- package swing.day01;
-
- import awt.day01.Utils;
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.util.Objects;
-
- public class InputDialogTest {
-
- JFrame frame = new JFrame("输入对话框");
- JTextArea textArea = new JTextArea(8,20);
- JButton input=new JButton(new AbstractAction("输入对话框") {
- @Override
- public void actionPerformed(ActionEvent e) {
- String s = JOptionPane.showInputDialog(frame, "请填写您的银行卡账号", "输入对话框", JOptionPane.QUESTION_MESSAGE);
- if (Objects.nonNull(s)) {
- textArea.append(s);
- }
-
- }
- });
- JButton choose=new JButton(new AbstractAction("输入对话框") {
- @Override
- public void actionPerformed(ActionEvent e) {
- Object o = JOptionPane.showInputDialog(frame, "", "输入对话框", JOptionPane.DEFAULT_OPTION, null, new String[]{"淀粉肠", "火腿肠", "纯肉肠"}, "纯肉肠");
- if (Objects.nonNull(o)) {
- textArea.append(o.toString());
- }
- }
- });
- JPanel panel = new JPanel();
-
- public void init(){
- frame.add(textArea);
- frame.add(panel,BorderLayout.SOUTH);
- panel.add(input);
- panel.add(choose);
-
- Utils.setJFrame(frame);
- }
-
- public static void main(String[] args) {
- new InputDialogTest().init();
- }
- }

- package swing.day01;
-
- import awt.day01.Utils;
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
-
- public class OptionDialogTest {
- JFrame frame = new JFrame("选项对话框");
- JTextArea textArea = new JTextArea(8,20);
- JButton button=new JButton(new AbstractAction("弹出选项对话框") {
- @Override
- public void actionPerformed(ActionEvent e) {
- int i = JOptionPane.showOptionDialog(frame, "请选择尿不湿号码", "选项对话框", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, new String[]{"小号", "中号", "大号"}, "中号");
- System.out.println(i);
- if (i == 0) {
- textArea.append("用户选择了小号\n");
- } else if (i == 1) {
- textArea.append("用户选择了中号\n");
- } else if (i == 2) {
- textArea.append("用户选择了大号\n");
- }else{
- textArea.append("用户未选择\n");
- }
- }
- });
-
- public void init(){
- frame.add(textArea);
- textArea.setText("Hello China\n");
- frame.add(button, BorderLayout.SOUTH);
-
- Utils.setJFrame(frame);
- }
-
- public static void main(String[] args) {
- new OptionDialogTest().init();
- }
- }