SHARE
TWEET

Untitled

a guest Sep 17th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package javafxcalcuator;
  7.  
  8. import java.net.URL;
  9. import java.util.ResourceBundle;
  10. import javafx.event.ActionEvent;
  11. import javafx.fxml.FXML;
  12. import javafx.fxml.Initializable;
  13. import javafx.scene.control.Label;
  14. import javafx.scene.control.TextField;
  15.  
  16. /**
  17.  *
  18.  * @author
  19.  */
  20. public class CalculatorFXMLDocumentController implements Initializable {
  21.    
  22.     @FXML
  23.     private TextField textField;
  24.    
  25.     @FXML
  26.     private void handleOneButtonAction(ActionEvent event) {
  27.         //add "1" to the text field
  28.         if(textField.getText().equals("") ) { //text field is emtpy
  29.             textField.setText("1");
  30.         } else {
  31.             String current = textField.getText();
  32.             textField.setText(current + "1");
  33.         }
  34.     }
  35.    
  36.     @FXML
  37.     private void handleTwoButtonAction(ActionEvent event) {
  38.         //add "2" to the text field
  39.         if(textField.getText().equals("") ) { //text field is emtpy
  40.             textField.setText("2");
  41.         } else {
  42.             String current = textField.getText();
  43.             textField.setText(current + "2");
  44.         }
  45.     }
  46.    
  47.     @FXML
  48.     private void handleThreeButtonAction(ActionEvent event) {
  49.         //add "3" to the text field
  50.         if(textField.getText().equals("") ) { //text field is emtpy
  51.             textField.setText("3");
  52.         } else {
  53.             String current = textField.getText();
  54.             textField.setText(current + "3");
  55.         }
  56.     }
  57.    
  58.     @FXML
  59.     private void handleFourButtonAction(ActionEvent event) {
  60.         //add "4" to the text field
  61.         if(textField.getText().equals("") ) { //text field is emtpy
  62.             textField.setText("4");
  63.         } else {
  64.             String current = textField.getText();
  65.             textField.setText(current + "4");
  66.         }
  67.     }
  68.    
  69.     @FXML
  70.     private void handleFiveButtonAction(ActionEvent event) {
  71.         //add "5" to the text field
  72.         if(textField.getText().equals("") ) { //text field is emtpy
  73.             textField.setText("5");
  74.         } else {
  75.             String current = textField.getText();
  76.             textField.setText(current + "5");
  77.         }
  78.     }
  79.    
  80.     @FXML
  81.     private void handleSixButtonAction(ActionEvent event) {
  82.         //add "6" to the text field
  83.         if(textField.getText().equals("") ) { //text field is emtpy
  84.             textField.setText("6");
  85.         } else {
  86.             String current = textField.getText();
  87.             textField.setText(current + "6");
  88.         }
  89.     }
  90.    
  91.     @FXML
  92.     private void handleSevenButtonAction(ActionEvent event) {
  93.         //add "7" to the text field
  94.         if(textField.getText().equals("") ) { //text field is emtpy
  95.             textField.setText("7");
  96.         } else {
  97.             String current = textField.getText();
  98.             textField.setText(current + "7");
  99.         }
  100.     }
  101.    
  102.     @FXML
  103.     private void handleEightButtonAction(ActionEvent event) {
  104.         //add "8" to the text field
  105.         if(textField.getText().equals("") ) { //text field is emtpy
  106.             textField.setText("8");
  107.         } else {
  108.             String current = textField.getText();
  109.             textField.setText(current + "8"
  110.                     + "");
  111.         }
  112.     }
  113.    
  114.     @FXML
  115.     private void handleNineButtonAction(ActionEvent event) {
  116.         //add "9" to the text field
  117.         if(textField.getText().equals("") ) { //text field is emtpy
  118.             textField.setText("9");
  119.         } else {
  120.             String current = textField.getText();
  121.             textField.setText(current + "9");
  122.         }
  123.     }
  124.    
  125.     @FXML
  126.     private void handleDecimalButtonAction(ActionEvent event) {
  127.         //add "2" to the text field
  128.         if(textField.getText().equals("") ) { //text field is emtpy
  129.             textField.setText(".");
  130.         } else {
  131.             String current = textField.getText();
  132.             textField.setText(current + ".");
  133.         }
  134.     }
  135.    
  136.     @FXML
  137.     private void handlePlusButtonAction(ActionEvent event) {
  138.         //add "+" to the text field
  139.         if (! (textField.getText().equals("")) ) { //if textfield is not empty
  140.             String current = textField.getText();
  141.             textField.setText(current + " + ");
  142.         }
  143.     }
  144.    
  145.     @FXML
  146.     private void handleMinusButtonAction(ActionEvent event) {
  147.         //add "+" to the text field
  148.         if (! (textField.getText().equals("")) ) { //if textfield is not empty
  149.             String current = textField.getText();
  150.             textField.setText(current + " - ");
  151.         }
  152.     }
  153.    
  154.     @FXML
  155.     private void handleTimesButtonAction(ActionEvent event) {
  156.         //add "+" to the text field
  157.         if (! (textField.getText().equals("")) ) { //if textfield is not empty
  158.             String current = textField.getText();
  159.             textField.setText(current + " * ");
  160.         }
  161.     }
  162.    
  163.     @FXML
  164.     private void handleDivisonButtonAction(ActionEvent event) {
  165.         //add "+" to the text field
  166.         if (! (textField.getText().equals("")) ) { //if textfield is not empty
  167.             String current = textField.getText();
  168.             textField.setText(current + " / ");
  169.         }
  170.     }
  171.    
  172.     @FXML
  173.     private void handleEqualButtonAction(ActionEvent event) {
  174.         //retrieve operands and operator
  175.         String formula = textField.getText();
  176.        
  177.         int firstSpace = formula.indexOf(" ");
  178.         int secondSpace = firstSpace + 2;
  179.        
  180.         double leftOperand = Double.parseDouble(formula.substring(0, firstSpace) );
  181.         double rightOperand = Double.parseDouble(formula.substring(secondSpace+1) );
  182.         char operator = formula.charAt(firstSpace + 1);
  183.        
  184.         //calculate the result
  185.         double result = 0;
  186.         if (operator == '+') {
  187.             result = leftOperand + rightOperand;
  188.         }
  189.        
  190.         //show the result
  191.         textField.setText("" + result);
  192.        
  193.     }
  194.    
  195.     @FXML
  196.     private void handleTextFieldButtonAction(ActionEvent event) {
  197.         handleEqualButtonAction(event);
  198.     }
  199.    
  200.     @FXML
  201.     private void handleClearButtonAction(ActionEvent event) {
  202.         textField.setText("");
  203.     }
  204.    
  205.     @Override
  206.     public void initialize(URL url, ResourceBundle rb) {
  207.         // TODO
  208.     }    
  209.    
  210. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top