简体   繁体   English

如何检测JTextField是否为空?

[英]How to detect whether JTextField is blank?

I have made a chat client application and I am stuck on the login dialog. 我已经创建了一个聊天客户端应用程序,但被卡在了登录对话框中。 Basically, I want the register button event to detect whether the user entered anything into the field box. 基本上,我希望注册按钮事件能够检测用户是否在字段框中输入了任何内容。 This code isn't working. 该代码不起作用。

 public class RegButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { if(userBox.getText() != null) { System.out.println("Lol"); } } } 

TRy this code 尝试此代码

public class RegButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource == userBox) {
           if(!userBox.getText().isEmpty())
           {
            System.out.println("Lol");
           }
        }
    }
}

You can check if it's empty (in this case, not empty) in two ways: using isEmpty( ) or equals("") 您可以通过两种方式检查它是否为空(在这种情况下为非空):使用isEmpty( )或equals("")

if (!userBox.getText().isEmpty()){
    //code
}

or 要么

if (!userBox.getText().equals("")){
    //code
}

Try this.. 尝试这个..

 if(!userBox.getText().equals("")) { System.out.println("ABC"); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM