如果文本字段没有值以忽略该方法,我如何创建 if 语句?

How can I create an if statement if there are no values for the textfield to ignore the method?

如果 TextField 中有值,我只想打印以下项目,但我找不到答案。以前我使用 Checkboxvar.isSelected() 方法来切换这些按钮,但看起来它的设计和功能似乎没有太大影响,我想我可以删除它并应用不同的方法来函数。

如果文本字段中没有值,程序应忽略该语句并转到另一个。

对不起我的英语。

我的代码:

    try{dishCarbonara = Double.parseDouble(dishCarbonaraTF.getText());}
        catch(NumberFormatException e){dishCarbonaraTF.setText("0");}
    dishes += dishCarbonara * 35 ;
    listItems += "Carbonara: 35 * "+dishCarbonara+"\n";
    
    try{dishChickenWing = Double.parseDouble(dshChickenWIngsTF.getText());}
        catch(NumberFormatException e){dshChickenWIngsTF.setText("0");} 
    dishes += 50 * dishChickenWing;
    listItems += "Chicken Wings: 30 * "+dishChickenWing+"\n";
    
    try{dishPotatoFries = Double.parseDouble(dishPotatoFriesTF.getText());}
        catch(NumberFormatException e){dishPotatoFriesTF.setText("0");} 
    dishes += 25 * dishPotatoFries;
    listItems += "Potato Fries: 25 * "+dishPotatoFries+"\n";

简单的 if 检查怎么样?

if (!dishCarbonaraTF.getText().equals("")) {
    // text field is not empty do something with the value 
}

当然,您会为每个 TextField 执行此操作。

您还可以执行以下操作以确保输入的不仅仅是空格:

if (!dishCarbonaraTF.getText().trim().equals("")) {
    // textfield is not empty and doesn't contain any white spaces
}