如何检查JTextField的文字为String? [英] How to check JTextField text to String?

查看:178
本文介绍了如何检查JTextField的文字为String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对检查文本和字符串的一个问题。

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

这是我迄今。

推荐答案

由于你的问题不是很清楚,我建议这些问题的答案:

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

第一种选择 - 你想从你的JTextField中得到的字符串:

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]+$")){...

第三选项 - 要比较两个字符串(其中一个是从JTextField中):

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

这篇关于如何检查JTextField的文字为String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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