将TextField添加到JOptionPane

时间:2015-10-20 23:35:42

标签: java string swing jtextfield joptionpane

我正在开发一个简单的程序,显示" Hello"并使用JOptionPane接受用户输入。我想阅读用户输入并将其与显示的单词进行比较。例如,程序将显示" Hello"并且用户必须在文本框中输入单词。如果他们输入"你好"然后"纠正"将打印。如果他们没有输入Hello,那么"不正确"将打印。为了读取用户输入并比较两个字符串,我需要做什么?

public static void main(String[] args){

    String resp = "Hello";

    JOptionPane.showInputDialog(null, resp);

    String input = ; //what should go here                              

    if (resp.compareTo(input) == 0) {
        JOptionPane.showMessageDialog(null, "Correct!");
        } else
        JOptionPane.showMessageDialog(null, "Incorrect");
        }
    }
}

1 个答案:

答案 0 :(得分:4)

public static void main(String[] args)
{

String resp = "Hello";

String input = JOptionPane.showInputDialog(null, resp);                         

if (resp.compareTo(input) == 0)
    JOptionPane.showMessageDialog(null, "Correct!");
else
    JOptionPane.showMessageDialog(null, "Incorrect");
}
相关问题