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);
}
}
That's mainly a Java programming problem. If you want to retrieve the value of a
TextField
, the methodmyTextField.getText()
is what you are looking for.To have a better UI, you could allow user to only type numbers