Java Swing – JOptionPane showOptionDialog示例
程序员文章站
2024-02-02 10:31:16
...
JOptionPane
的showOptionDialog
方法是showConfirmDialog,showInputDialog和showMessageDialog的统一。
showOptionDialog
返回一个integer
,该integer
表示用户选择的Object[]
在Object[]
。
注意
如果您想了解更多有关不同showXxxDialog方法的信息,请参阅
如果您想了解更多有关不同showXxxDialog方法的信息,请参阅
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);
}
}
输出:
单击“ 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");
}
}
}
输出:
勾选复选框并显示“ 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!");
}
}
}
输出:
单击Mary,Nora,Anna或X(关闭窗口)时:
单击Lauren时:
图片来源:https://en.wikipedia.org/wiki/File:Creative-Tail-People-girl.svg
参考文献
标签: 对话框 JOptionPane 摇摆
From: https://mkyong.com/swing/java-swing-joptionpane-showoptiondialog-example/
上一篇: IDEA打jar包(带外部依赖)
下一篇: IDEA+Maven 打jar包
推荐阅读
-
Java Swing – JOptionPane showOptionDialog示例
-
Java Swing – JOptionPane showInputDialog示例
-
Java Swing实现让窗体居中显示的方法示例
-
Java Swing组件文件选择器JFileChooser简单用法示例
-
java Swing JFrame框架类中setDefaultCloseOperation的参数含义与用法示例
-
Java Swing实现JTable检测单元格数据变更事件的方法示例
-
Java Swing中JList选择事件监听器ListSelectionListener用法示例
-
Java Swing中JDialog实现用户登陆UI示例
-
Java Swing中JTable渲染器与编辑器用法示例
-
Java Swing组件单选框JRadioButton用法示例