下拉对话框

时间:2013-03-20 11:39:06

标签: java swing dialog

我从oracle网站获得了一个下拉框示例:

Object[] possibilities = {"ham", "spam", "yam"};
String s = (String)JOptionPane.showInputDialog(
                frame,
                "Complete the sentence:\n"
                + "\"Green eggs and...\"",
                "Customized Dialog",
                JOptionPane.PLAIN_MESSAGE,
                icon,
                possibilities,
                "ham");

//If a string was returned, say so.
if ((s != null) && (s.length() > 0)) {
setLabel("Green eggs and... " + s + "!");
return;
}

//If you're here, the return value was null/empty.
setLabel("Come on, finish the sentence!");

我只是想知道你如何改变每个选择会做什么?例如,我需要在语句if ((s != null) && (s.length() > 0)) {中更改某些内容,以便我可以编辑第一个字符串项吗?

1 个答案:

答案 0 :(得分:0)

检查用户选择的项目并执行某项操作非常简单

if ((s != null) && (s.length() > 0)) {

     if(s.equals(possibilities[0]);
         //HAM is chosen do something
     else if(s.equals(possibilities[1]);
         //spam and so on..........
     return;
}