Java Swing – JOptionPane showOptionDialog示例

JOptionPaneshowOptionDialog方法是showConfirmDialog,showInputDialog和showMessageDialog的统一。

showOptionDialog返回一个integer ,该integer表示用户选择的Object[]Object[]

注意
如果您想了解更多有关不同showXxxDialog方法的信息,请参阅

  1. Java Swing – JOptionPane showConfirmDialog示例
  2. Java Swing – JOptionPane showInputDialog示例
  3. Java Swing – JOptionPane showMessageDialog示例

1. showConfirmDialog具有不同的数组类型

options array可以设置为您选择的任何类型。

ConfirmDialog1a.java
package com.mkyong.optiondialog;

import javax.swing.*;

public class ConfirmDialog1a {

    public static void main(String[] args) {

        String[] options = {"abc", "def", "ghi", "jkl"};
        //Integer[] options = {1, 3, 5, 7, 9, 11};
        //Double[] options = {3.141, 1.618};
        //Character[] options = {'a', 'b', 'c', 'd'};
        int x = JOptionPane.showOptionDialog(null, "Returns the position of your choice on the array",
                "Click a button",
                JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
        System.out.println(x);

    }
}

输出:

Java Swing – JOptionPane showOptionDialog示例_第1张图片 Java Swing – JOptionPane showOptionDialog示例_第2张图片

单击“ abc”时:

0

单击“ def”时:

1

单击“ ghi”时:

2

单击“ jkl”时:

3

您也可以将数组设置为Object[]

ConfirmDialog1b.java
package com.mkyong.optiondialog;

import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

public class ConfirmDialog1b {

    public static void main(String[] args) {

        JCheckBox check = new JCheckBox("Tick me");
        Object[] options = {'e', 2, 3.14, 4, 5, "TURTLES!", check};
        int x = JOptionPane.showOptionDialog(null, "So many options using Object[]",
                "Don't forget to Tick it!",
                JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);

        if (check.isSelected() && x != -1) {
            System.out.println("Your choice was " + options[x]);
        } else {
            System.out.println(":( no choice");
        }

    }

}

输出:

Java Swing – JOptionPane showOptionDialog示例_第3张图片 Java Swing – JOptionPane showOptionDialog示例_第4张图片

勾选复选框并显示“ TURTLES!”时 点击:

Your choice was TURTLES!

当窗口关闭或用户单击按钮而不勾选复选框时:

:( no choice

2. showConfirmDialog

showConfirmDialog放置在框架中并将图标设置为本地图片文件的示例。

ConfirmDialog2.java
package com.mkyong.optiondialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

@SuppressWarnings("serial")
public class ConfirmDialog2 extends JFrame {

    public ConfirmDialog2() {

        getContentPane().setBackground(new Color(238, 232, 170));
        getContentPane().setLayout(null);
        setTitle("Confirm Dialog in Frame");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        setSize(450, 300);

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(dim.width / 2 - this.getWidth() / 2, dim.height / 2 - this.getHeight() / 2);

    }

    public JPanel panel() {

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 444, 271);
        panel.setBackground(new Color(176, 224, 230));
        getContentPane().add(panel);
        panel.setLayout(null);

        JLabel lblIcon = new JLabel("");
        lblIcon.setBounds(30, 30, 200, 200);
        lblIcon.setIcon(new ImageIcon("src/images/girl200.png"));
        panel.add(lblIcon);

        JLabel lblText2 = new JLabel("Lauren is the 4th daughter!");
        lblText2.setVerticalAlignment(SwingConstants.TOP);
        lblText2.setFont(new Font("Tahoma", Font.ITALIC, 14));
        lblText2.setHorizontalAlignment(SwingConstants.CENTER);
        lblText2.setBounds(240, 130, 175, 100);
        panel.add(lblText2);

        JLabel lblText1 = new JLabel("Yaaaay!");
        lblText1.setHorizontalAlignment(SwingConstants.CENTER);
        lblText1.setFont(new Font("Tahoma", Font.ITALIC, 14));
        lblText1.setBounds(240, 30, 175, 100);
        panel.add(lblText1);

        return panel;

    }

    public static void main(String[] args) {

        ConfirmDialog2 cdframe = new ConfirmDialog2();
        ImageIcon icon = new ImageIcon("src/images/girl64.png");
        String[] options = {"Mary", "Nora", "Anna", "Lauren"};
        int x = JOptionPane.showOptionDialog(cdframe, "Lauren's mom had four kids: Maria, Martha, Margaret...",
                "The missing kid", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, icon, options, options[0]);

        if (x == 3) {
            cdframe.getContentPane().add(cdframe.panel());
            cdframe.repaint();
            cdframe.revalidate();
        } else {
            cdframe.dispose();
            JOptionPane.showMessageDialog(null, "Nooope!");
        }

    }
}

输出:

Java Swing – JOptionPane showOptionDialog示例_第5张图片 Java Swing – JOptionPane showOptionDialog示例_第6张图片

单击Mary,Nora,Anna或X(关闭窗口)时:

Java Swing – JOptionPane showOptionDialog示例_第7张图片 Java Swing – JOptionPane showOptionDialog示例_第8张图片

单击Lauren时:

Java Swing – JOptionPane showOptionDialog示例_第9张图片 Java Swing – JOptionPane showOptionDialog示例_第10张图片

图片来源:https://en.wikipedia.org/wiki/File:Creative-Tail-People-girl.svg

参考文献

  1. 如何制作对话框
  2. JOption窗格类– Java 8 API

翻译自: https://mkyong.com/swing/java-swing-joptionpane-showoptiondialog-example/

你可能感兴趣的:(Java Swing – JOptionPane showOptionDialog示例)