JTextfield.getText().equals() 方法无效
JTextfield.getText().equals() method not working
我正在尝试使用以下代码检查 jTextfield 是否为空,如果为空则显示错误对话框。
if(sproductname.getText().equals("")){
JOptionPane.showMessageDialog(null,"One or more fields is empty","Empty field",JOptionPane.ERROR_MESSAGE);
}
该方法在 actionperformed 中,应该在我按下按钮时执行,但在这种情况下没有任何反应。这么小的一段代码能出什么问题?
试试这个:
if (sproductname.getText().isEmpty()){
...
}
您的代码无法正常工作,因为对象类型字符串的基本值为空。不是空字符串。 Java 中的所有对象类型都具有基本值 == null。
我正在尝试使用以下代码检查 jTextfield 是否为空,如果为空则显示错误对话框。
if(sproductname.getText().equals("")){
JOptionPane.showMessageDialog(null,"One or more fields is empty","Empty field",JOptionPane.ERROR_MESSAGE);
}
该方法在 actionperformed 中,应该在我按下按钮时执行,但在这种情况下没有任何反应。这么小的一段代码能出什么问题?
试试这个:
if (sproductname.getText().isEmpty()){
...
}
您的代码无法正常工作,因为对象类型字符串的基本值为空。不是空字符串。 Java 中的所有对象类型都具有基本值 == null。