Skip to content

Instantly share code, notes, and snippets.

@argius
Last active Aug 23, 2021
Embed
What would you like to do?
Calculator written in Java 8 with JavaFX 8 + SceneBuilder 2.0
package net.argius.calculator;
import java.math.BigDecimal;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public final class App extends Application {
private BigDecimal left;
private String selectedOperator;
private boolean numberInputting;
@FXML
private TextField display;
public App() {
this.left = BigDecimal.ZERO;
this.selectedOperator = "";
this.numberInputting = false;
}
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("Calculator");
stage.setOnCloseRequest(x -> {
Platform.exit();
});
stage.setResizable(false);
stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("main.fxml"))));
stage.show();
}
@FXML
protected void handleOnAnyButtonClicked(ActionEvent evt) {
Button button = (Button)evt.getSource();
final String buttonText = button.getText();
if (buttonText.equals("C") || buttonText.equals("AC")) {
if (buttonText.equals("AC")) {
left = BigDecimal.ZERO;
}
selectedOperator = "";
numberInputting = false;
display.setText("0");;
return;
}
if (buttonText.matches("[0-9\\.]")) {
if (!numberInputting) {
numberInputting = true;
display.clear();
}
display.appendText(buttonText);
return;
}
if (buttonText.matches("[+-×÷]")) {
left = new BigDecimal(display.getText());
selectedOperator = buttonText;
numberInputting = false;
return;
}
if (buttonText.equals("=")) {
final BigDecimal right = numberInputting ? new BigDecimal(display.getText()) : left;
left = calculate(selectedOperator, left, right);
display.setText(left.toString());
numberInputting = false;
return;
}
}
static BigDecimal calculate(String operator, BigDecimal left, BigDecimal right) {
switch (operator) {
case "+":
return left.add(right);
case "-":
return left.subtract(right);
case "×":
return left.multiply(right);
case "÷":
return left.divide(right);
default:
}
return right;
}
public static void main(String[] args) {
launch(args);
}
}
.background {
-fx-background-color: #663333;
}
.button {
-fx-background-color: silver;
}
.display {
-fx-background-color: #cccccc;
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="249.0" prefWidth="244.0" styleClass="background" stylesheets="@main.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="net.argius.calculator.App">
<children>
<Button layoutX="71.0" layoutY="171.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="1" />
<Button layoutX="106.0" layoutY="171.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="2" />
<Button layoutX="140.0" layoutY="171.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="3" />
<Button layoutX="71.0" layoutY="134.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="4" />
<Button layoutX="106.0" layoutY="134.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="5" />
<Button layoutX="140.0" layoutY="134.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="6" />
<Button layoutX="71.0" layoutY="97.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="7" />
<Button layoutX="106.0" layoutY="97.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="8" />
<Button layoutX="140.0" layoutY="97.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="9" />
<TextField fx:id="display" alignment="BOTTOM_RIGHT" editable="false" layoutX="31.0" layoutY="14.0" prefHeight="35.0" prefWidth="178.0" styleClass="display" text="0">
<font>
<Font name="Lucida Console" size="20.0" />
</font>
</TextField>
<Button layoutX="27.0" layoutY="97.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="C" />
<Button layoutX="167.0" layoutY="208.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" prefHeight="23.0" prefWidth="47.0" text="=" />
<Button layoutX="187.0" layoutY="171.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="" />
<Button layoutX="71.0" layoutY="208.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="0" />
<Button layoutX="187.0" layoutY="134.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="" />
<Button layoutX="188.0" layoutY="97.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" prefHeight="23.0" prefWidth="25.0" styleClass="background" text="×" />
<Button layoutX="188.0" layoutY="61.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" styleClass="background" text="÷" />
<Button layoutX="25.0" layoutY="61.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="AC" />
<Button layoutX="142.0" layoutY="208.0" mnemonicParsing="false" onAction="#handleOnAnyButtonClicked" text="." />
</children>
</AnchorPane>
@MirandinhaKL
Copy link

MirandinhaKL commented Sep 5, 2018

Thank you!
Great code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment