简体   繁体   中英

How to have the java applet run a different calculation depending on which JTextfields are filled?

I am fairly new to java (and stack overflow) so please bear with me. I am trying to create a physics projectile motion calculator which also outputs a graph of the motion. I want to have JTextfields in which the user can enter some or all of the following conditions: initial angle, initial speed, mass, range, maximum height. If the user does not enter all of the values, I want the program to fill in the remaining fields. So for example, if the user enters initial angle, initial speed and mass then I want it to calculate the range and max height. I know how to do everything described except one thing: I don't know how to have the program confirm which fields are filled and then, based on that, run the necessary calculation/method to find the missing values . I know how to use .trim().isEmpty() and I thought maybe I could use that along with some kind of boolean array or something? I searched around and couldn't find any answers. Anybody have any ideas?

Any help appreciated! Let me know if I need to provide a SSCCE (I didn't think it would be especially helpful for this question)

Edit: It seems my wording was unclear, I apologize for that. I know how to determine if a field is empty, what I need to know is how to have the program decide what calculation method to run depending on the combination of filled/unfilled fields. This is because the equations are such that I need at least 3 out of the 5 variables to calculate the others.

So for example, if the user enters the mass, angle and range then I need to call a specific method to calculate the height and initial speed. However if the user enters mass, angle and speed then I need to call a different method to calculate the height and range. Note that although height is missing in both of the above cases, I need to call different methods in each case because the combination of filled/unfilled fields is different.

JTextfield inputMass, inputAngle, inputSpeed, inputRange, inputHeight:

JButton goButton;

public void init(){
      //setup GUI
      goButton.addActionListener(this);
}

public void actionPerformed(ActionEvent click){
        Object src = click.getSource();
        if(src == goButton){
        boolean isMass = inputMass.trim().isEmpty;
//repeat for all fields
//some code to call a certain method based on which fields are filled
  }
}

I am thinking about making a boolean array and calling a method if the array matches so and so (note that there are ten possible combinations of three filled fields).

Using .trim().isEmpty() would work. Try implementing something as follows:

if (massTextField.getText().trim().isEmpty()) {
    //Call method to determine mass.
}
else if (rangeTextField.getText().trim().isEmpty()) {
    //Call method to determine range.
}

...and so on, for each TextField you want to test.

Hope this helps.

EDIT:

The boolean array idea would work. I cannot think of a very nice, flexible way to implement this solution, as it requires that you know exactly which variables are missing, and must calculate the answer differently depending on that case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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