package zikao.swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class OptionDialogTest {
JFrame jFrame = new JFrame("测试选项对话框");
JTextArea jta = new JTextArea(6, 30);
JButton btn = new JButton(new AbstractAction("弹出选项对话框") {
@Override
public void actionPerformed(ActionEvent e) {
int result = JOptionPane.showOptionDialog(jFrame, "请选择奶茶大小", "选项对话框",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new String[]{"小杯", "中杯", "大杯"}, "中杯");
jta.append("\n");
switch (result) {
case 0 -> jta.append("用户选择了小杯\n");
case 1 -> jta.append("用户选择了中杯\n");
case 2 -> jta.append("用户选择了大杯\n");
}
}
});
private void init() {
jFrame.add(jta);
jFrame.add(btn, BorderLayout.SOUTH);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}
public static void main(String[] args) {
new OptionDialogTest().init();
}
}
JAVA SWING 实现选择器可视化
最新推荐文章于 2024-08-04 14:58:40 发布