A
A
Andrey Sokolov2020-02-29 19:35:32
Java
Andrey Sokolov, 2020-02-29 19:35:32

How to get data from javafx TextField?

After I filled in the two fields that are located at the top of the program, from the text field No. 1 (Amount of attachment), the number should be multiplied by the number from the second text field and then the user clicks on the button, and the result is displayed even in the text field No. 3 5e5a908d49dc6619018775.png

Here is what is in files:

Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Calculator");
        primaryStage.setScene(new Scene(root, 529, 414));
        primaryStage.show();
    }


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


Controller.java
package sample;

public class Controller {
}


sample.fxml
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="502.0" prefWidth="529.0" style="-fx-background-color: #FAF9F0;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane prefHeight="104.0" prefWidth="529.0" style="-fx-background-color: #00B850;">
         <children>
            <Label layoutX="97.0" prefHeight="0.0" prefWidth="336.0" text="КАЛЬКУЛЯТОР" textFill="WHITE">
               <font>
                  <Font name="VAG World Bold" size="47.0" />
               </font>
            </Label>
         </children>
      </AnchorPane>
      <TextField layoutX="106.0" layoutY="134.0" opacity="0.8" prefHeight="70.0" prefWidth="300.0" promptText="Сумма вложения">
         <font>
            <Font name="VAG World Bold" size="21.0" />
         </font>
      </TextField>
      <TextField layoutX="106.0" layoutY="215.0" opacity="0.8" prefHeight="70.0" prefWidth="300.0" promptText="Срок в месяцах">
         <font>
            <Font name="VAG World Bold" size="21.0" />
         </font>
      </TextField>
      <Button layoutX="106.0" layoutY="303.0" mnemonicParsing="false" prefHeight="70.0" prefWidth="300.0" style="-fx-background-color: #00B850; -fx-cursor: hand;" text="Посчитать" textFill="WHITE">
         <font>
            <Font name="VAG World Bold" size="30.0" />
         </font>
      </Button>
      <TextField disable="true" layoutX="106.0" layoutY="404.0" opacity="0.8" prefHeight="70.0" prefWidth="300.0" promptText="Результат">
         <font>
            <Font name="VAG World Bold" size="21.0" />
         </font>
      </TextField>
   </children>
</AnchorPane>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question