1

我想访问用户选择的选项的索引。例如,在下图中,如果我选择 microsoft 选项,那么它应该给我索引 1。这可能吗?

在此处输入图像描述

4

1 回答 1

2

好吧,您得到"Microsoft"Object至少显示 Microsoft)作为 show 调用的返回值,这是否足够好?

如果您需要索引,只需在您提供给对话框的输入数组中找到该返回值的索引。

请参阅 java 教程的输入部分:http: //docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input

假设您使用的是 showInputDialog(..):

Object[] possibilities = {"Broadcom...", "Microsoft"};
Object result = JOptionPane.showInputDialog( frame, "Capture Interfaces", "Input",   JOptionPane.PLAIN_MESSAGE, icon, possibilities, possibilities[0]);

if (result != null) {
    //result is the choosen object, if you need the index even so:
    int index = 0;
    for (Object o : possibilities) {
        if (result == o)
            break;
        index++
    }
    //index is now the index...
}
于 2012-05-20T12:47:08.000 回答