简体   繁体   中英

How to pass TextField info to handle() method

Beginner question: The last part of my assignment is to change the handle() method so when the button is clicked, it returns the result for a miles per gallon calculator. How do I get the user input from the mi and gal TextFields when that part is coded outside of the handle method?

public class MPGApplication extends Application {

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Miles Per Gallon");

        Button btn = new Button("Calculate");
        HBox hbBtn = new HBox(10);
        hbBtn.setAlignment(Pos.BOTTOM_LEFT);
        hbBtn.getChildren().add(btn);
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

            }
        });

        GridPane root = new GridPane();
        root.setAlignment(Pos.TOP_CENTER);
        root.setHgap(10);
        root.setVgap(10);
        root.setPadding(new Insets(25, 25, 25, 25));
        root.add(hbBtn, 1, 3);

        Scene scene = new Scene(root, 300, 250);
        primaryStage.setScene(scene);

        Label miles = new Label("Miles:");
        root.add(miles, 0, 0);
        Label gallons = new Label("Gallons:");
        root.add(gallons, 0, 1);
        Label mpg = new Label("MPG:");
        root.add(mpg, 0, 2);

        TextField mi = new TextField();
        root.add(mi, 1, 0);
        TextField gal = new TextField();
        root.add(gal, 1, 1);
        TextField gpm = new TextField();
        gpm.setEditable(false);
        root.add(gpm, 1, 2);

        primaryStage.show();
    }

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

}

You could declare and initialize the TextFields above the setOnAction method of Button, that would make the TextField instance visible to the button handler method.

Or to keep a bit better readability - just move the handling method to the bottom like:

btn.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        System.out.println(mi.getText());
        System.out.println(gal.getText());
        System.out.println(gpm.getText());
    }
});
primaryStage.show();

The thing here is that, the method local variables needs to be declared (and mostly initialized) before making any reference to them (ie using them).

Where in class you could declare members at the bottom of class definition (but the practice is to declare them on top).

@Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
root.setAlignment(Pos.TOP_CENTER);
root.setHgap(10);
root.setVgap(10);
root.setPadding(new Insets(25, 25, 25, 25));


Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);

Label miles = new Label("Miles:");
root.add(miles, 0, 0);
Label gallons = new Label("Gallons:");
root.add(gallons, 0, 1);
Label mpg = new Label("MPG:");
root.add(mpg, 0, 2);

TextField mi = new TextField();
root.add(mi, 1, 0);
TextField gal = new TextField();
root.add(gal, 1, 1);
TextField gpm = new TextField();
gpm.setEditable(false);
root.add(gpm, 1, 2);


primaryStage.setTitle("Miles Per Gallon");

Button btn = new Button("Calculate");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_LEFT);
hbBtn.getChildren().add(btn);
btn.setOnAction(new EventHandler<ActionEvent>() {

    @Override
    public void handle(ActionEvent event) {
        double temp = Integer.parseInt(mi.getText());
        double temp2 =Integer.parseInt(gal.getText());
        String temp3=Double.toString(temp/temp2);
        gpm.setText(temp3);
    }
});
root.add(hbBtn, 1, 3);`
primaryStage.show();
}

//create all the label and scene first then tells the button to do the division works

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