关于Java中JTextField的一个问题
尽管设计非常简单,但在运行我编写的 Java 类时遇到了一个小问题。我创建了一个 JPanel,并向其添加了四个 JTextField,并且还向该 JPanel 附加了一个按钮。然后,我将 ActionListener 与按下的按钮关联起来。代码如下:
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (imageIdField.getText() == "" &&
captionField.getText() == "" &&
creditField.getText() == "" &&
titleField.getText()== "")
{
mediaXML = "";
results.clear();
results.put("error1", "more");
}
else
{ ....
}
}
奇怪的是,在我按下“确定”按钮后,我在这四个 JTextField 中输入了文本,但它仍然会落在 IF 分支中,就好像我没有在这四个 JTextField 中的任何一个中输入任何文本一样字段。 我已经调试了一段时间,但没有任何线索。谁能给我一些提示,例如 .getText() == "" 是否是测试无输入的有效方法?
提前致谢!
I met a small problem when running a Java class that I wrote, though the design is pretty straightforward. I've created a JPanel, and I've added four JTextFields onto it and I've attached a button to this JPanel, too. Then, I've associated an ActionListener to this button being pressed. The code is like:
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if (imageIdField.getText() == "" &&
captionField.getText() == "" &&
creditField.getText() == "" &&
titleField.getText()== "")
{
mediaXML = "";
results.clear();
results.put("error1", "more");
}
else
{ ....
}
}
The strange thing is after I've pressed the OK button, and I did input text in those four JTextFields, still it will fall in the IF branch as if I didn't input any text in any of these four fields.
I've been debugging this for a while, but no clue. Could anyone give me some hint like whether .getText() == "" is a valid way for testing no input?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
正如已经提到的,使用
==
是不正确的。为了便于阅读,请尝试:或
As has been mentioned, using
==
is not correct. For readability, try:or
通常,在
String
或大多数其他事物上使用==
是个坏主意。它检查对象是否是完全相同的实例,而不是它们具有相同的值。"" != new String("")
。或者可能更好:
Generally a bad idea to use
==
onString
s, or most other things. It checks that the objects are exactly the same instance, not that they have the same value."" != new String("")
.Or possibly better:
使用
getText().equals("")
而不是==
Use
getText().equals("")
instead of==
使用 == 检查内存中是否是同一个对象,使用 .equals("YOUR STRING") 检查对象的内容是否相同。
Use == to check if it is the same object in memory, and .equals("YOUR STRING") to check if the content of the object is the same.
您应该使用 .等于。另外,您可能想做这样的事情:
或者
如果您想确保用户实际上写了一些字符而不仅仅是空格。
You should use .equals. Also, you might want to do something like this:
or
if you want to make sure that the user has actually written some characters instead of just white spaces.
==
仅检查左侧和右侧是否引用对象的完全相同的实例。由于""
会翻译为new String("")
之类的内容,因此如果将其与已存在的字符串进行比较,它将始终返回 false。如果您想比较一个类的两个实例是否具有相同的状态,您需要使用
equals()
。在您的情况下*.getText().equals("")
。更优雅的方法是使用 String 类的 isEmpty() 方法。==
only checks whether the left hand side and the right hand side refer to the exact same instance of an object. And since""
translates to something likenew String("")
, it will always return false, if you compare it with a string that already exists.If you want to compare whether two instances of a class have the same state you need to use
equals()
. In your case*.getText().equals("")
. A more elegant method would to use theisEmpty()
method of the String class.