Created
August 26, 2021 16:36
-
-
Save Da9el00/a1f33e45bb68b3f3354ae033ce0c7639 to your computer and use it in GitHub Desktop.
JavaFX and Scene Builder - Calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package sample; | |
| import javafx.event.ActionEvent; | |
| import javafx.fxml.FXML; | |
| import javafx.scene.control.TextField; | |
| import javafx.scene.text.Text; | |
| public class Controller { | |
| @FXML | |
| private TextField textField; | |
| @FXML | |
| private Text savedNumbers; | |
| private String firstNumber = ""; | |
| private String currentNumber = ""; | |
| private String calculationType; | |
| @FXML | |
| void addAction(ActionEvent event) { | |
| calculationSetup("+"); | |
| } | |
| @FXML | |
| void minusAction(ActionEvent event) { | |
| calculationSetup("-"); | |
| } | |
| @FXML | |
| void divideAction(ActionEvent event) { | |
| calculationSetup("/"); | |
| } | |
| @FXML | |
| void multiplicationAction(ActionEvent event) { | |
| calculationSetup("*"); | |
| } | |
| public void calculationSetup(String calculationType){ | |
| this.calculationType = calculationType; | |
| firstNumber = currentNumber; | |
| currentNumber = ""; | |
| savedNumbers.setText(firstNumber + " " + calculationType); | |
| } | |
| @FXML | |
| void calculate(ActionEvent event) { | |
| int firstNumberInt = Integer.parseInt(firstNumber); | |
| int secondNumberInt = Integer.parseInt(currentNumber); | |
| switch (calculationType) { | |
| case "+" -> { | |
| int calculatedNumber = firstNumberInt + secondNumberInt; | |
| savedNumbers.setText(firstNumber + " + " + currentNumber + " = " + calculatedNumber); | |
| textField.setText(String.valueOf(calculatedNumber)); | |
| } | |
| case "-" -> { | |
| int calculatedNumber = firstNumberInt - secondNumberInt; | |
| savedNumbers.setText(firstNumber + " - " + currentNumber + " = " + calculatedNumber); | |
| textField.setText(String.valueOf(calculatedNumber)); | |
| } | |
| case "/" -> { | |
| double calculatedNumber = firstNumberInt / (double)secondNumberInt; | |
| savedNumbers.setText(firstNumber + " / " + currentNumber + " = " + calculatedNumber); | |
| textField.setText(String.valueOf(calculatedNumber)); | |
| } | |
| case "*" -> { | |
| int calculatedNumber = firstNumberInt * secondNumberInt; | |
| savedNumbers.setText(firstNumber + " * " + currentNumber + " = " + calculatedNumber); | |
| textField.setText(String.valueOf(calculatedNumber)); | |
| } | |
| } | |
| } | |
| @FXML | |
| void clearTextField(ActionEvent event) { | |
| currentNumber = ""; | |
| textField.setText(""); | |
| savedNumbers.setText(""); | |
| } | |
| @FXML | |
| void button0Clicked(ActionEvent event) { | |
| if(!currentNumber.equals("")){ | |
| addNumber("0"); | |
| } | |
| } | |
| @FXML | |
| void button1Clicked(ActionEvent event) { | |
| addNumber("1"); | |
| } | |
| @FXML | |
| void button2Clicked(ActionEvent event) { | |
| addNumber("2"); | |
| } | |
| @FXML | |
| void button3Clicked(ActionEvent event) { | |
| addNumber("3"); | |
| } | |
| @FXML | |
| void button4Clicked(ActionEvent event) { | |
| addNumber("4"); | |
| } | |
| @FXML | |
| void button5Clicked(ActionEvent event) { | |
| addNumber("5"); | |
| } | |
| @FXML | |
| void button6Clicked(ActionEvent event) { | |
| addNumber("6"); | |
| } | |
| @FXML | |
| void button7Clicked(ActionEvent event) { | |
| addNumber("7"); | |
| } | |
| @FXML | |
| void button8Clicked(ActionEvent event) { | |
| addNumber("8"); | |
| } | |
| @FXML | |
| void button9Clicked(ActionEvent event) { | |
| addNumber("9"); | |
| } | |
| public void updateTextField(){ | |
| textField.setText(currentNumber); | |
| } | |
| public void addNumber(String number){ | |
| currentNumber += number; | |
| updateTextField(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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("Hello World"); | |
| primaryStage.setScene(new Scene(root)); | |
| primaryStage.show(); | |
| } | |
| public static void main(String[] args) { | |
| launch(args); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <?import javafx.scene.control.Button?> | |
| <?import javafx.scene.control.TextField?> | |
| <?import javafx.scene.layout.AnchorPane?> | |
| <?import javafx.scene.layout.FlowPane?> | |
| <?import javafx.scene.layout.VBox?> | |
| <?import javafx.scene.text.Font?> | |
| <?import javafx.scene.text.Text?> | |
| <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> | |
| <children> | |
| <TextField fx:id="textField" editable="false" layoutX="25.0" layoutY="15.0" nodeOrientation="RIGHT_TO_LEFT" prefHeight="75.0" prefWidth="250.0" text="0"> | |
| <font> | |
| <Font size="40.0" /> | |
| </font> | |
| </TextField> | |
| <FlowPane layoutX="25.0" layoutY="125.0" prefHeight="180.0" prefWidth="180.0"> | |
| <children> | |
| <Button mnemonicParsing="false" onAction="#button1Clicked" prefHeight="60.0" prefWidth="60.0" text="1"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="10.0" layoutY="10.0" mnemonicParsing="false" onAction="#button2Clicked" prefHeight="60.0" prefWidth="60.0" text="2"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="70.0" layoutY="10.0" mnemonicParsing="false" onAction="#button3Clicked" prefHeight="60.0" prefWidth="60.0" text="3"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="130.0" layoutY="10.0" mnemonicParsing="false" onAction="#button4Clicked" prefHeight="60.0" prefWidth="60.0" text="4"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="10.0" layoutY="70.0" mnemonicParsing="false" onAction="#button5Clicked" prefHeight="60.0" prefWidth="60.0" text="5"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="70.0" layoutY="70.0" mnemonicParsing="false" onAction="#button6Clicked" prefHeight="60.0" prefWidth="60.0" text="6"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="130.0" layoutY="70.0" mnemonicParsing="false" onAction="#button7Clicked" prefHeight="60.0" prefWidth="60.0" text="7"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="10.0" layoutY="130.0" mnemonicParsing="false" onAction="#button8Clicked" prefHeight="60.0" prefWidth="60.0" text="8"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="70.0" layoutY="130.0" mnemonicParsing="false" onAction="#button9Clicked" prefHeight="60.0" prefWidth="60.0" text="9"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="70.0" layoutY="10.0" mnemonicParsing="false" onAction="#button0Clicked" prefHeight="60.0" prefWidth="60.0" text="0"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button mnemonicParsing="false" onAction="#calculate" prefHeight="60.0" prefWidth="60.0" text="="> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button mnemonicParsing="false" onAction="#clearTextField" prefHeight="60.0" prefWidth="60.0" text="C"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| </children> | |
| </FlowPane> | |
| <VBox layoutX="215.0" layoutY="125.0" prefHeight="240.0" prefWidth="60.0"> | |
| <children> | |
| <Button mnemonicParsing="false" onAction="#addAction" prefHeight="60.0" prefWidth="60.0" text="+"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="10.0" layoutY="70.0" mnemonicParsing="false" onAction="#minusAction" prefHeight="60.0" prefWidth="60.0" text="-"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="10.0" layoutY="130.0" mnemonicParsing="false" onAction="#divideAction" prefHeight="60.0" prefWidth="60.0" text="/"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| <Button layoutX="10.0" layoutY="172.0" mnemonicParsing="false" onAction="#multiplicationAction" prefHeight="60.0" prefWidth="60.0" text="*"> | |
| <font> | |
| <Font size="25.0" /> | |
| </font> | |
| </Button> | |
| </children> | |
| </VBox> | |
| <Text fx:id="savedNumbers" layoutX="30.0" layoutY="30.0" strokeType="OUTSIDE" strokeWidth="0.0" /> | |
| </children> | |
| </AnchorPane> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very helpful. clean