且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java&&关于if语句不起作用

更新时间:2021-12-17 14:42:10

您的语句运行正常.编写方式只有在两个字段均为空时(如果jTextFieldAccountName为空且jPasswordFieldAccountPassword长度等于0),该按钮才被禁用.在第一个字段中键入内容时,两个字段都不再为空,因此条件设置为false,并且启用了按钮.

Your statement is working fine. The way you have it written, the button is going to be disabled only when BOTH fields are empty (if jTextFieldAccountName is empty AND jPasswordFieldAccountPassword length equals 0). When you type something into the first field, both fields are no longer empty so the condition sets false, and your button is enabled.

如果您希望在启用按钮之前同时输入两个字段,请将逻辑更改为:

If you want both fields to be input before the button is enabled, change your logic to:

if ((!jTextFieldAccountName.getText().isEmpty()) && (jPasswordFieldAccountPassword.getPassword().length > 0)) {
            jButton_Next.setEnabled(true);
        }
        else {
            jButton_Next.setEnabled(false);
        }

如果使用此逻辑,还可以将密码设置为最小长度(例如,密码> 6,或类似的值).

If you use this logic, you can also set your password to be a minimum length (e.g. password > 6, or something like that).