简体   繁体   中英

Adding a TextField to JOptionPane

I am working on a simple program that displays "Hello" and accepts user input using JOptionPane. I wanted to read the users input and compare it to the word that is displayed. For example, the program will display "Hello" and the user would have to input a word into the text box. If they type "Hello" then "Correct" will print. If they don't type Hello then "Incorrect" will print. In order to read the users input and compare the two strings, what do I need to do?

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");
        }
    }
}
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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM