如何检查 JTextField 文本到字符串?

发布于 2024-12-06 02:31:54 字数 302 浏览 3 评论 0原文

我在检查文本和字符串时遇到问题。

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

这是我到目前为止所拥有的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

得不到的就毁灭 2024-12-13 02:31:54

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

第一个选项 - 您想从 JTextField 获取字符串:

String text = txtGuessPad.getText();

第二个选项 - 您想检查文本是否仅包含字母:

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

第三个选项 - 您想比较两个字符串(其中之一)它们来自 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

第四个选项 - 让我们把它放在一个函数中:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文