简体   繁体   English

如果JTextField不包含数据,则显示消息对话框

[英]Display message dialog if JTextField does not contain data

I am writing a BMI calculator application. 我正在编写一个BMI计算器应用程序。 Currently an error happens which causes the program to stop working if I do not enter data into one field. 当前发生错误,如果我没有在一个字段中输入数据,则会导致程序停止运行。 For instance, there are two JTextFIelds for 'height', one being feet and the other inches. 例如,对于“ height”有两个JTextFIeld,一个是英尺,另一个是英寸。 If I just input '6' into the feet JTextField and enter nothing into inches JTextField, then enter my weight in the weight JTextField and click on calculate, it does not work. 如果我仅在脚部JTextField中输入“ 6”,而在英寸JTextField中未输入任何内容,然后在重量JTextField中输入我的体重,然后单击“计算”,它将不起作用。

What I want to do is display a message dialog saying "Please make sure all fields are filled in" if one field does not contain data. 我想要做的是显示一个消息对话框,如果其中一个字段不包含数据,则显示“请确保所有字段都已填写”。

Below is the ActionHandler code that is added to my 'Calculate' button. 以下是添加到“计算”按钮的ActionHandler代码。

public void actionPerformed(ActionEvent e) {
    double heightFT = ((Double.parseDouble(heightFt_TF.getText()));
    double heightIn = (Double.parseDouble(heightIn_TF.getText()));
    double weight = (Double.parseDouble(weight_TF.getText()));
    double totalHeight = (heightFT*12) + heightIn;              

    BMI = (weight / (totalHeight*totalHeight)) * 703;
    String s = BMI+"";
    s = s.substring(0,4);
    BMI_TF.setText(s);                              
}

Solved 解决了

I have now fixed the problem. 我现在已经解决了问题。 What I did was add 'throws NumberFormatException' in the method and did a try catch. 我所做的是在方法中添加“ throws NumberFormatException”并进行了尝试捕获。 In the try code block I wrote the code I want to execute if all data fields are entered. 如果输入所有数据字段,我在try代码块中编写了要执行的代码。 In the catch clause I wrote code that uses the NumberFormatException and simply displays the message dialog with the error message. 在catch子句中,我编写了使用NumberFormatException的代码,并仅显示带有错误消息的消息对话框。 Now, if one field is not entered, the message dialog appears! 现在,如果未输入一个字段,将显示消息对话框!

Just check if your JTextField objects contain text. 只要检查您的JTextField对象是否包含文本即可。 Eg: 例如:

if (heightFt_TF.getText() == null || heightIn_TF.getText() == null || weight_TF.getText() == null) {
JOptionPane.showMessageDialog(null, "Please make sure all fields are filled in");
}

Of course you also have to make sure, that the content of the textfields really contains a number. 当然,您还必须确保文本字段的内容确实包含数字。

Download Apache Commons Lang library and use StringUtils.isBlank(myTextField.getText()); 下载Apache Commons Lang库并使用StringUtils.isBlank(myTextField.getText()); to validate your fields. 验证您的字段。

public boolean validateFields() {
    if (StringUtils.isBlank(heightFt_TF.getText()) {
        // show message
        return false;
    }

    if (StringUtils.isBlank(weight_TF.getText()) {
        // show message
        return false;
    }

    return true;
}

Only run your calculation if validateFields() returns true. 仅当validateFields()返回true时才运行计算。

public boolean validate(JTextField field) {

  boolean result = field.getText() != null;
  if (result) {
    try {
      Double.parseDouble(field.getText()));
    } catch(NumberFormatException e) {
      result = false
    } 
  }
  return result;                    
}

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

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