关于Java中JTextField的一个问题

发布于 2024-10-07 16:57:29 字数 847 浏览 3 评论 0原文

尽管设计非常简单,但在运行我编写的 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 技术交流群。

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

发布评论

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

评论(6

走过海棠暮 2024-10-14 16:57:29

正如已经提到的,使用 == 是不正确的。为了便于阅读,请尝试:

field.getText().isEmpty()

field.getText().trim().isEmpty()

As has been mentioned, using == is not correct. For readability, try:

field.getText().isEmpty()

or

field.getText().trim().isEmpty()
命硬 2024-10-14 16:57:29

通常,在 String 或大多数其他事物上使用 == 是个坏主意。它检查对象是否是完全相同的实例,而不是它们具有相同的值。 "" != new String("")

field.getText().equals("")

或者可能更好:

field.getText().isEmpty()

Generally a bad idea to use == on Strings, or most other things. It checks that the objects are exactly the same instance, not that they have the same value. "" != new String("").

field.getText().equals("")

Or possibly better:

field.getText().isEmpty()
铜锣湾横着走 2024-10-14 16:57:29

使用 getText().equals("") 而不是 ==

Use getText().equals("") instead of ==

蓦然回首 2024-10-14 16:57:29

使用 == 检查内存中是否是同一个对象,使用 .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.

幽梦紫曦~ 2024-10-14 16:57:29

您应该使用 .等于。另外,您可能想做这样的事情:

imageField.getText().trim().length() == 0  //The same for the others

或者

imageField.getText().trim().isEmpty() //The same for the others

如果您想确保用户实际上写了一些字符而不仅仅是空格。

You should use .equals. Also, you might want to do something like this:

imageField.getText().trim().length() == 0  //The same for the others

or

imageField.getText().trim().isEmpty() //The same for the others

if you want to make sure that the user has actually written some characters instead of just white spaces.

静谧幽蓝 2024-10-14 16:57:29

== 仅检查左侧和右侧是否引用对象的完全相同的实例。由于 "" 会翻译为 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 like new 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 the isEmpty() method of the String class.

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