在这里寻找我的代码的一些帮助。我正在尝试创建一个JOptionPane
输入对话框,该对话框将接收来自用户的输入(选项1或选项2)并根据第一个输入显示下一个菜单。 1和2会有不同的结果。
我能以正确的方式解决这个问题吗?
代码:
public class MyJavaApplication {
public static void main(String[] args) throws FileNotFoundException {
//1. Options
List<String> optionList = new ArrayList<String>(); **//Create an array to store user input**
optionList.add("1");
optionList.add("2");
Object[] options = optionList.toArray(); **//Store option into array**
Object value = JOptionPane.showInputDialog(null,
"Please select your encryption Algorithm:(1 or 2)\n 1. Data Encryption Standard(DES) \n 2. Advanced Encryption Standard(AES)",
null,
options,
options[0],
options[1]); **//JOption input dialog asking for either option one or 2**
int index = optionList.indexOf(value);
编辑:
if (value == 1) {
List<String> optionList2 = new ArrayList<String>();
optionList2.add("ECB");
optionList2.add("CBC");
Object[] options2 = optionList2.toArray();
int value2 = JOptionPane.showOptionDialog(null,
"Please select your mode of operation:\n 1. Cipher Block Chaining(CBC) \n 2. Electronic Codebook(ECB)",
"Select",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
optionList2.get(0));
String option2 = optionList2.get(value2);
}}
答案 0 :(得分:2)
不确定为什么要尝试使用输入对话框。如果对话框被解除,输入对话框将返回输入到输入字段中的值或null
,这似乎不是您真正想要的...
相反,请尝试使用标准输入对话框....
List<String> optionList = new ArrayList<String>();
optionList.add("1");
optionList.add("2");
Object[] options = optionList.toArray();
int value = JOptionPane.showOptionDialog(
null,
"Please select your encryption Algorithm:(1 or 2)\n 1. Data Encryption Standard(DES) \n 2. Advanced Encryption Standard(AES)",
"Pick",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
optionList.get(0));
String opt = optionList.get(value);
System.out.println("You picked " + opt);