从Joptionpane获取索引 [英] Obtaining index from the Joptionpane

查看:64
本文介绍了从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆