从Joptionpane获取索引 [英] Obtaining index from the Joptionpane
问题描述
我要访问用户选择的选项的索引.例如,在下图中,如果我选择microsoft option,那么它应该给我索引1.这可能吗?
好吧,您获得了"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...
}
I want to access the index of the option selected by the user.Eg, in the figure below,if i choose microsoft option then it should give me the index 1.Is this possible?
Well you get "Microsoft"
(well the Object
showing Microsoft at least) as the return value from the show call, is that good enough?
If you need the index just find the index of that return value in the input array you provided to the dialog.
See the input section of the java tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input
Assuming you are using 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...
}
这篇关于从Joptionpane获取索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!