Created
July 6, 2017 03:24
-
-
Save Bhavesh-Joshi/96d3a6b7d369fc528d15e3b1231550d6 to your computer and use it in GitHub Desktop.
Calculator using JavaFX in a simple way
This file contains 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
.root{ | |
-fx-background-color: darkgrey; | |
} | |
ToggleButton{ | |
-fx-padding: 15; | |
-fx-font-size: 15; | |
} |
This file contains 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 Calculator; | |
import javafx.geometry.Insets; | |
import javafx.geometry.Pos; | |
import javafx.scene.control.TextField; | |
import javafx.scene.control.ToggleButton; | |
import javafx.scene.control.ToggleGroup; | |
import javafx.scene.layout.HBox; | |
import javafx.scene.layout.VBox; | |
public class Display { | |
static String a, b, operator;//a=first number && b=second number | |
static int time = 0; | |
public static VBox numpad() { | |
TextField input = new TextField(); | |
input.setPromptText("Enter a value"); | |
VBox lay = new VBox(); | |
lay.setAlignment(Pos.CENTER); | |
lay.getChildren().add(input); | |
ToggleGroup tg = new ToggleGroup(); | |
String[][] s = { | |
{"9", "8", "7", "/"}, | |
{"6", "5", "4", "*"}, | |
{"1", "2", "3", "-"}, | |
{"0", ".", "=", "+"} | |
}; | |
for (int i = 0; i < 4; i++) { | |
HBox h = new HBox(); | |
for (int j = 0; j < 4; j++) { | |
ToggleButton t = new ToggleButton(s[i][j]); | |
t.setUserData(s[i][j]); | |
t.setToggleGroup(tg); | |
h.getChildren().add(t); | |
} | |
h.setPadding(new Insets(10, 0, 0, 0)); | |
h.setSpacing(10); | |
h.setAlignment(Pos.CENTER); | |
lay.getChildren().add(h); | |
} | |
tg.selectedToggleProperty().addListener((v, oldValue, newValue) -> { | |
String data = newValue.getUserData().toString(); | |
if (data.equals("+") || data.equals("-") || data.equals("*") || data.equals("/") || data.equals("=")) { | |
if (time == 0) { | |
a = input.getText(); | |
operator=data; | |
input.clear(); | |
time++; | |
} else { | |
b = input.getText(); | |
input.clear(); | |
calculation(input); | |
time=0; | |
} | |
} else | |
input.setText(input.getText() + data); | |
}); | |
return lay; | |
} | |
public static void calculation(TextField input) { | |
Double ans=0.0; | |
switch (operator) { | |
case "+": ans = Double.parseDouble(a) + Double.parseDouble(b); | |
break; | |
case "-": ans = Double.parseDouble(a) - Double.parseDouble(b); | |
break; | |
case "*": ans = Double.parseDouble(a) + Double.parseDouble(b); | |
break; | |
case "/": ans = Double.parseDouble(a) + Double.parseDouble(b); | |
break; | |
} | |
input.setText(""+ans); | |
} | |
} |
This file contains 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 Calculator; | |
import javafx.application.Application; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.VBox; | |
import javafx.stage.Stage; | |
public class Main extends Application{ | |
public static void main(String[] args){ | |
launch(args); | |
} | |
public void start(Stage window) throws Exception{ | |
VBox lay = Display.numpad(); | |
window.setTitle("Calculator"); | |
Scene sc = new Scene(lay, 230, 300); | |
sc.getStylesheets().add("Calculator.css"); | |
window.setScene(sc); | |
window.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment