简体   繁体   中英

JavaFX, TextFields, EventHandler

I'm just learning JavaFX and watching tutorials. Its a lot of information to process, so I'm starting off with a simple BMI Calculator app. Im not including the BMI class, but it has a constructor of: BMI(double height, double weight, String gender)

Problems:

  1. My EventHandler is not working. I tried to do the same as a tutorial, but I can't get it work. I tried with a simple Print if eventSource = button

  2. So I will have 3 TextFields where the user enters the 3 specified values, height, weight gender. When the user does this, I want to use my BMI class and create a new BMI using these 3 values. I found a methode: "textfield.getAccesibleText(); Maybe that could work but I cant find out since my eventhandler isn't working.

All in all, the user should press the "Calculate" button after entering the 3 values, I will then create a Bmi using my Bmi class, and display the value of that Bmi in a new box. (I will have to add the box where Bmi) is displayed later.

Get Eventhandler working, being able to take values from textfields (like scanner) , create a new Bmi, create a new box where I will display the value of that Bmi. (I got a methode in my BMI class which calculates the BMI

public class Main extends Application  {
Button b1;
Button b2;
Text g;
Text w; 
Text h; 

TextField g1; 
TextField w1;
TextField h1; 
@Override


public void start(Stage stage) throws Exception{

 g = new Text("Gender:");
 w = new Text("Weight:");
 h = new Text("Height");
 g1 = new TextField();
 w1 = new TextField();
 h1 = new TextField();



b1 = new Button("Calculate BMI!");
b2 = new Button("Reset BMI");

GridPane grid = new GridPane();

grid.setMinSize(400, 200);

grid.setPadding(new Insets(10, 10, 10, 10));
  grid.setVgap(5); 
  grid.setHgap(5);

  grid.setAlignment(Pos.CENTER); 

  grid.add(g, 0, 0);
  grid.add(g1, 1, 0);
  grid.add(w, 0, 1);
  grid.add(w1, 1, 1);
  grid.add(h, 0, 2);
  grid.add(h1, 1, 2);
  grid.add(b1, 0, 3);
  grid.add(b2, 1, 3);

  b1.setStyle("-fx-background-color: purple; -fx-text-fill: white;"); 
  b2.setStyle("-fx-background-color: purple; -fx-text-fill: white;");
  grid.setStyle("-fx-background-color: GREEN;"); 

  Scene scene = new Scene(grid);

  stage.setScene(scene);

  stage.show();


}


public void handle(ActionEvent event){
    if(event.getSource() == b1){
    //do something
}
}



public static void main(String[] args) {
    launch(args);



}}

It seem like you have the method to handle the action event but the handler itself is never attached to neither the buttons nor the text fields. In order to do so you need to provide an EventHandler interface implementation, for example:

button.addEventHandler(ActionEvent.ACTION, (ActionEvent event) -> {
    // your code here
});

See Working with Event Handlers for more details.

However as @kleopatra mentioned a better practice is to provide the event handler using the appropriate setOnXxx() method, for example:

button.setOnAction((ActionEvent event) -> {
    // your code here
});

Do you also know how to "gather" the user input in the textfiles, like a scanner, then create a new Bmi with them and finally display that number in a new box or something similar.

Here is the part where you need to figure out the business model in order to generate the class or classes that will hold the data from the controls. A good candidate is a BmiData POJO as follows:

public class BmiData {

    private String gender;
    private Double height;
    private Double weight;

    // Constructor, getters and setters

    public Double calculateBmi() {
        // perform the calculation here
    }
}

Then the event handler attached to the "Calculate BMI" button is a good candidate to collect the data from the text fields into a BmiData object and finally display the result to the user. I'd suggest you to look into MVC (Model-View-Controller) design pattern.

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