JavaFX Scene Builder Tutorial 34: Calculator
Learn to build a calculator with JavaFX Scene Builder. Code available at https://codebyamir.blogspot.com/ π±

Code Amir
30.8K views β’ Apr 14, 2020

About this video
JavaFX Scene Builder Tutorial 34 Calculator JavaFX
Code source here : https://codebyamir.blogspot.com/
@FXML
private TextField txt_result;
String op ="";
long number1;
long number2;
// First Method
public void Number (ActionEvent ae){
String no = ((Button)ae.getSource()).getText();
txt_result.setText(txt_result.getText()+no);
}
// Second Method
public void Operation (ActionEvent ae){
String operation = ((Button)ae.getSource()).getText();
if (!operation.equals("=")){
if(!op.equals("")){
return;
}
op = operation;
number1 = Long.parseLong(txt_result.getText());
txt_result.setText("");
}else {
if(op.equals("")){
return;
}
number2 = Long.parseLong(txt_result.getText());
calculate(number1, number2, op);
op="";
}
}
// 3 Methode
public void calculate (long n1, long n2, String op){
switch (op){
case "+" : txt_result.setText(n1 + n2 + "");break;
case "-" : txt_result.setText(n1 - n2 + "");break;
case "*" : txt_result.setText(n1 * n2 + "");break;
case "/" :
if (n2 == 0){
txt_result.setText("0");break;
}
txt_result.setText(n1/n2+ "");break;
}
}
Libraries Importing :
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
Code source here : https://codebyamir.blogspot.com/
@FXML
private TextField txt_result;
String op ="";
long number1;
long number2;
// First Method
public void Number (ActionEvent ae){
String no = ((Button)ae.getSource()).getText();
txt_result.setText(txt_result.getText()+no);
}
// Second Method
public void Operation (ActionEvent ae){
String operation = ((Button)ae.getSource()).getText();
if (!operation.equals("=")){
if(!op.equals("")){
return;
}
op = operation;
number1 = Long.parseLong(txt_result.getText());
txt_result.setText("");
}else {
if(op.equals("")){
return;
}
number2 = Long.parseLong(txt_result.getText());
calculate(number1, number2, op);
op="";
}
}
// 3 Methode
public void calculate (long n1, long n2, String op){
switch (op){
case "+" : txt_result.setText(n1 + n2 + "");break;
case "-" : txt_result.setText(n1 - n2 + "");break;
case "*" : txt_result.setText(n1 * n2 + "");break;
case "/" :
if (n2 == 0){
txt_result.setText("0");break;
}
txt_result.setText(n1/n2+ "");break;
}
}
Libraries Importing :
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
Tags and Topics
Browse our collection to discover more content in these categories.
Video Information
Views
30.8K
Likes
254
Duration
22:46
Published
Apr 14, 2020
User Reviews
4.3
(6) Related Trending Topics
LIVE TRENDSRelated trending topics. Click any trend to explore more videos.