我想在btnCurrentStatus
的点击事件上显示我的数组(已标记),然后我想从用户返回所选的值。我正在使用的代码如下所示,但这里通过showMessageDialog
方法我只能设法显示数组,我想要的是用户可以选择其中一个值,我想返回该索引。
如何实现?
btnCurrentStatus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int j = 0;
int c = 0;
for (int i = 0; i < total_question; i++) {
if (question_status[i] == 1) {
marked[j] = i + 1;
j++;
// System.out.println((i + 1) + " : Marked");
} else if (question_status[i] == 2) {
locked[k] = i + 1;
c++;
//System.out.println((i + 1) + " : Locked");
}
}
String display = "";
// String markedq []= new String[] {"1","2","3","4"};
for (int a = 0; a < marked.length; a++) {
if (marked[a] != 0) {
display += marked[a] + "\n";
}
}
JOptionPane.showMessageDialog(null, display);
}
});
答案 0 :(得分:4)
如果您想从用户那里获得选择,请不要使用JOptionPane.showMessageDialog(...)。而是使用不同的对话框,例如JOptionPane.showInputDialog:
// from the JOptionPane API
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);