Hello Friends Today I am going to share, how to create Calculator using JavaFX. This is a simple calculator with simple UI where you can Add, Substract, Mutiply, Divide, and calculate Percentage.
So here is the code
CalculatorUI.fxml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<?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.BorderPane?> <?import javafx.scene.layout.ColumnConstraints?> <?import javafx.scene.layout.GridPane?> <?import javafx.scene.layout.RowConstraints?> <?import javafx.scene.text.Font?> <AnchorPane id="AnchorPane" prefHeight="300.0" prefWidth="335.0" xmlns="http://javafx.com/javafx/8.0.101" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zatackcoder.CalculatorUIController"> <children> <BorderPane prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <top> <TextField fx:id="outputTF" alignment="CENTER_RIGHT" promptText="0"> <font> <Font size="30.0" /> </font> </TextField> </top> <center> <GridPane BorderPane.alignment="CENTER"> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> </columnConstraints> <rowConstraints> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> </rowConstraints> <children> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="7" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="8" GridPane.columnIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="9" GridPane.columnIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="\%" GridPane.columnIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="4" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="5" GridPane.columnIndex="1" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="6" GridPane.columnIndex="2" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="X" GridPane.columnIndex="3" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="1" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="2" GridPane.columnIndex="1" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="3" GridPane.columnIndex="2" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="-" GridPane.columnIndex="3" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onCEClick" text="CE" GridPane.columnIndex="4" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="/" GridPane.columnIndex="4" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="=" GridPane.columnIndex="4" GridPane.rowIndex="2" GridPane.rowSpan="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="." GridPane.rowIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="0" GridPane.columnIndex="1" GridPane.rowIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onDELClick" text="DEL" GridPane.columnIndex="2" GridPane.rowIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="+" GridPane.columnIndex="3" GridPane.rowIndex="3" /> </children> </GridPane> </center> </BorderPane> </children> </AnchorPane> |
CalculatorUIController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.zatackcoder; import java.net.URL; import java.util.ResourceBundle; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.TextField; /** * FXML Controller class * * @author rajesh */ public class CalculatorUIController implements Initializable { Double temp = 0.0, sum = 0.0; boolean isOperatorPressed; String operatorPressed = ""; @FXML TextField outputTF; /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) { outputTF.textProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { if (!newValue.matches("\\d*([\\.]\\d*)?")) { outputTF.setText(oldValue); } } }); } @FXML private void onNumberClick(ActionEvent event) { if(event.getSource() instanceof Button) { Button btn = (Button)event.getSource(); if(isOperatorPressed) { outputTF.setText(btn.getText().trim()); } else { outputTF.setText(outputTF.getText().trim() + btn.getText().trim()); } isOperatorPressed = false; } } @FXML private void onOperatorClick(ActionEvent event) { if(event.getSource() instanceof Button) { Button btn = (Button)event.getSource(); if (!outputTF.getText().isEmpty()) { temp = Double.valueOf(outputTF.getText()); if (btn.getText().equals("%")) { temp = sum * temp / 100; } switch (operatorPressed) { case "/": sum /= temp; break; case "X": sum *= temp; break; case "+": sum += temp; break; case "-": sum -= temp; break; default: sum = temp; } } if (btn.getText().equals("=") || btn.getText().equals("%")) { outputTF.setText(String.valueOf(sum)); operatorPressed = ""; } else { outputTF.setText(""); operatorPressed = btn.getText().trim(); } isOperatorPressed = true; } } @FXML private void onDELClick(ActionEvent event) { if(outputTF.getText().length() > 0) { outputTF.setText(outputTF.getText(0, outputTF.getText().length() - 1)); } } @FXML private void onCEClick(ActionEvent event) { outputTF.setText(""); temp = 0.0; sum = 0.0; isOperatorPressed = false; operatorPressed = ""; } } |
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.zatackcoder; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; /** * * @author rajesh */ public class Main extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("CalculatorUI.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } } |
Output/Screen Shot
Download NetBeans Project
Calculator in JavaFX
1 file(s) 184.55 KB
Thank you
Please share
Comments