gpt4 book ai didi

java - JTextfield,如何验证getText()方法中的内容

转载 作者:行者123 更新时间:2023-12-02 00:43:09 25 4
gpt4 key购买 nike

我有一个名为 x 的文本字段。

当文本字段包含“”时,我想做一些事情。如果没有,请执行其他操作。

我尝试这样做

String test = x.getText();
if(test.startsWith(" ")){buttonN.setForeground(Color.GRAY));}
else{buttonN.setForeground(Color.BLACK));}

但是没有成功。任何建议

最佳答案

为什么不使用contains?:

 if(x.getText().contains("\u0020"))
buttonN.setForeground(Color.GRAY);
else
buttonN.setForeground(Color.BLACK);

虽然上述方法可以工作,但它不会检测表格间距。话虽这么说,我建议使用正则表达式来代替:

if(Pattern.compile("\\s").matcher(x.getText()).find())
buttonN.setForeground(Color.GRAY);
else
buttonN.setForeground(Color.BLACK);

Reference .

关于java - JTextfield,如何验证getText()方法中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5751415/

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