Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 javafxcalcuator;
- import java.net.URL;
- import java.util.ResourceBundle;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.Label;
- import javafx.scene.control.TextField;
- /**
- *
- * @author
- */
- public class CalculatorFXMLDocumentController implements Initializable {
- @FXML
- private TextField textField;
- @FXML
- private void handleOneButtonAction(ActionEvent event) {
- //add "1" to the text field
- if(textField.getText().equals("") ) { //text field is emtpy
- textField.setText("1");
- } else {
- String current = textField.getText();
- textField.setText(current + "1");
- }
- }
- @FXML
- private void handleTwoButtonAction(ActionEvent event) {
- //add "2" to the text field
- if(textField.getText().equals("") ) { //text field is emtpy
- textField.setText("2");
- } else {
- String current = textField.getText();
- textField.setText(current + "2");
- }
- }
- @FXML
- private void handleThreeButtonAction(ActionEvent event) {
- //add "3" to the text field
- if(textField.getText().equals("") ) { //text field is emtpy
- textField.setText("3");
- } else {
- String current = textField.getText();
- textField.setText(current + "3");
- }
- }
- @FXML
- private void handleFourButtonAction(ActionEvent event) {
- //add "4" to the text field
- if(textField.getText().equals("") ) { //text field is emtpy
- textField.setText("4");
- } else {
- String current = textField.getText();
- textField.setText(current + "4");
- }
- }
- @FXML
- private void handleFiveButtonAction(ActionEvent event) {
- //add "5" to the text field
- if(textField.getText().equals("") ) { //text field is emtpy
- textField.setText("5");
- } else {
- String current = textField.getText();
- textField.setText(current + "5");
- }
- }
- @FXML
- private void handleSixButtonAction(ActionEvent event) {
- //add "6" to the text field
- if(textField.getText().equals("") ) { //text field is emtpy
- textField.setText("6");
- } else {
- String current = textField.getText();
- textField.setText(current + "6");
- }
- }
- @FXML
- private void handleSevenButtonAction(ActionEvent event) {
- //add "7" to the text field
- if(textField.getText().equals("") ) { //text field is emtpy
- textField.setText("7");
- } else {
- String current = textField.getText();
- textField.setText(current + "7");
- }
- }
- @FXML
- private void handleEightButtonAction(ActionEvent event) {
- //add "8" to the text field
- if(textField.getText().equals("") ) { //text field is emtpy
- textField.setText("8");
- } else {
- String current = textField.getText();
- textField.setText(current + "8"
- + "");
- }
- }
- @FXML
- private void handleNineButtonAction(ActionEvent event) {
- //add "9" to the text field
- if(textField.getText().equals("") ) { //text field is emtpy
- textField.setText("9");
- } else {
- String current = textField.getText();
- textField.setText(current + "9");
- }
- }
- @FXML
- private void handleDecimalButtonAction(ActionEvent event) {
- //add "2" to the text field
- if(textField.getText().equals("") ) { //text field is emtpy
- textField.setText(".");
- } else {
- String current = textField.getText();
- textField.setText(current + ".");
- }
- }
- @FXML
- private void handlePlusButtonAction(ActionEvent event) {
- //add "+" to the text field
- if (! (textField.getText().equals("")) ) { //if textfield is not empty
- String current = textField.getText();
- textField.setText(current + " + ");
- }
- }
- @FXML
- private void handleMinusButtonAction(ActionEvent event) {
- //add "+" to the text field
- if (! (textField.getText().equals("")) ) { //if textfield is not empty
- String current = textField.getText();
- textField.setText(current + " - ");
- }
- }
- @FXML
- private void handleTimesButtonAction(ActionEvent event) {
- //add "+" to the text field
- if (! (textField.getText().equals("")) ) { //if textfield is not empty
- String current = textField.getText();
- textField.setText(current + " * ");
- }
- }
- @FXML
- private void handleDivisonButtonAction(ActionEvent event) {
- //add "+" to the text field
- if (! (textField.getText().equals("")) ) { //if textfield is not empty
- String current = textField.getText();
- textField.setText(current + " / ");
- }
- }
- @FXML
- private void handleEqualButtonAction(ActionEvent event) {
- //retrieve operands and operator
- String formula = textField.getText();
- int firstSpace = formula.indexOf(" ");
- int secondSpace = firstSpace + 2;
- double leftOperand = Double.parseDouble(formula.substring(0, firstSpace) );
- double rightOperand = Double.parseDouble(formula.substring(secondSpace+1) );
- char operator = formula.charAt(firstSpace + 1);
- //calculate the result
- double result = 0;
- if (operator == '+') {
- result = leftOperand + rightOperand;
- }
- //show the result
- textField.setText("" + result);
- }
- @FXML
- private void handleTextFieldButtonAction(ActionEvent event) {
- handleEqualButtonAction(event);
- }
- @FXML
- private void handleClearButtonAction(ActionEvent event) {
- textField.setText("");
- }
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- // TODO
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement