Javafx getting input from TextField

2019-06-14 02:10发布

问题:

This is my current code, all it does is set up a GUI interface for a calculator I made. I want the user to enter in two values and then when the "Sum" button is pressed it adds the two values together and displays it in the "Sum:" TextField. I'm experimenting with JavaFX, some help would be greatly appreciated.

import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.stage.*;
import javafx.scene.layout.*;
import javafx.geometry.*;
import javafx.event.*;


public class SimpleCalculator extends Application
{
    public void start(Stage myStage)
    {

        myStage.setTitle("Calculator");

        GridPane rootNode= new GridPane();
        rootNode.setPadding( new Insets( 15 ) );
        rootNode.setHgap( 5 );
        rootNode.setVgap( 5 );
        rootNode.setAlignment( Pos.CENTER );

        Scene myScene = new Scene( rootNode, 300, 200 );

        rootNode.add( new Label("First Value:"), 0,0); rootNode.add(new TextField(), 1, 0);
        rootNode.add( new Label ("Second Value:"), 0, 1); rootNode.add(new TextField(), 1, 1);
        rootNode.add( new Label("Sum is:"), 0, 2); rootNode.add(new TextField(), 1, 2);         
        Button aButton = new Button("Calculate"); rootNode.add(aButton, 1, 3);
        rootNode.setHalignment(aButton, HPos.LEFT);

        myStage.setScene( myScene);

        myStage.show();     
    }


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

}

回答1:

That's mainly a Java programming problem. If you want to retrieve the value of a TextField, the method myTextField.getText() is what you are looking for.

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class SimpleCalculator extends Application {
    @Override
    public void start(Stage myStage) {

        myStage.setTitle("Calculator");

        GridPane rootNode = new GridPane();
        rootNode.setPadding(new Insets(15));
        rootNode.setHgap(5);
        rootNode.setVgap(5);
        rootNode.setAlignment(Pos.CENTER);

        Scene myScene = new Scene(rootNode, 300, 200);

        rootNode.add(new Label("First Value:"), 0, 0);
        TextField firstValue = new TextField();
        rootNode.add(firstValue, 1, 0);
        rootNode.add(new Label("Second Value:"), 0, 1);
        TextField secondValue = new TextField();
        rootNode.add(secondValue, 1, 1);
        rootNode.add(new Label("Sum is:"), 0, 2);
        Button aButton = new Button("Calculate");
        rootNode.add(aButton, 1, 2);
        GridPane.setHalignment(aButton, HPos.LEFT);
        TextField result = new TextField();
        result.setEditable(false);
        rootNode.add(result, 1, 3);

        aButton.setOnAction(e -> {
            Integer value1 = Integer.valueOf(firstValue.getText());
            Integer value2 = Integer.valueOf(secondValue.getText());
            Integer r = value1 + value2;
            result.setText(r.toString());
        });

        myStage.setScene(myScene);

        myStage.show();
    }

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

}

To have a better UI, you could allow user to only type numbers