使用 showMessageDialog 和 return 选定值显示数组

Displaying a array using showMessageDialog and return selected value

我想在 btnCurrentStatus 的单击事件中显示我的数组(已标记),然后我想 return 来自用户的 selected 值。我为此使用的代码如下,但这里通过 showMessageDialog 方法我只能设法显示数组,我想要的是用户可以 select 其中一个值,我想 return那个索引。

如何实现?

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);
    }
});

如果您想从用户那里获得选择,请不要使用 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]);