How to check JTextField text to String?

风格不统一 提交于 2020-01-17 07:47:14

问题


I have a problem on checking the text and string.

public boolean Isequals(Object c){
    boolean check = false;
    if (c instanceof MyTextTwist){
        MyTextTwist tt = (MyTextTwist)c;
    if (txtGuessPad.getText().equals(answer))
        check = true;}
    return check;
    }

this what i have so far.


回答1:


Since your question is not very clear, i suggest these answers:

1st option - you want to get string from your JTextField:

String text = txtGuessPad.getText();

2nd option - you want to check if text contains only letter:

String text = txtGuessPad.getText();
if(text.matches("^[a-zA-Z]+$")){...

3rd option - you want to compare two strings (one of them is from JTextField):

String text = txtGuessPad.getText();
String text2 = "test";
if(text.equals(text2)){... //if you want to match whole word and case sensitive
if(text.equalsIgnoreCase(text2)){... //if you want to match whole word and NOT case sensitive
if(text.startsWith(text2)){... //if you want to check if you string starts with other string

4th option - let's put it in a function:

public boolean isEqualToString(JTextField textField, String compareTo) {
     String text = textField.getText();
     if(text.equals(compareTo)) {
         return true;
     }
     return false;
}


来源:https://stackoverflow.com/questions/7512999/how-to-check-jtextfield-text-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!