gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:43:56 25 4
gpt4 key购买 nike

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

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

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

最佳答案

由于您的问题不太清楚,我建议以下答案:

第一个选项 - 您想从 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;
}

关于java - 如何检查 JTextField 文本到字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7512999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com