JavaFX Scene Builder Calculator JavaFX

FXMLDocumentController.java package calculator; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEv...



FXMLDocumentController.java

package calculator;

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;

/**
 *
 * @author amir
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private TextField txt_result;
    
    String op ="";
    long number1;
    long number2;
    
    // click CTRL
    // 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;
        
        }
    
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
    
}



Class Calculator.java

package calculator;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author amir
 */
public class Calculator extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        
        Scene scene = new Scene(root);     
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}


FXMLDocument.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>


<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="calculator.FXMLDocumentController">
   <children>
      <TextField fx:id="txt_result" prefHeight="65.0" prefWidth="300.0">
         <VBox.margin>
            <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
         </VBox.margin>
         <font>
            <Font size="25.0" />
         </font>
      </TextField>
      <HBox prefHeight="40.0" prefWidth="300.0">
         <children>
            <Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="7">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="8">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="9">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#Operation" prefHeight="0.0" prefWidth="60.0" style="-fx-background-color: yellow;" text="/">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
         </children>
      </HBox>
      <HBox prefHeight="40.0" prefWidth="300.0">
         <children>
            <Button mnemonicParsing="false" onAction="#Number" prefHeight="45.0" prefWidth="60.0" text="4">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="5">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="6">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#Operation" prefHeight="0.0" prefWidth="60.0" style="-fx-background-color: red;" text="-">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
         </children>
      </HBox>
      <HBox prefHeight="40.0" prefWidth="300.0">
         <children>
            <Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="1">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="2">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#Number" prefHeight="0.0" prefWidth="60.0" text="3">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#Operation" prefHeight="0.0" prefWidth="60.0" style="-fx-background-color: pink;" text="*">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
         </children>
      </HBox>
      <HBox prefHeight="40.0" prefWidth="300.0">
         <children>
            <Button mnemonicParsing="false" onAction="#Number" prefHeight="48.0" prefWidth="138.0" text="0">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#Operation" prefHeight="0.0" prefWidth="60.0" style="-fx-background-color: Cyan;" text="=">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#Operation" prefHeight="0.0" prefWidth="60.0" style="-fx-background-color: green;" text="+">
               <HBox.margin>
                  <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
               </HBox.margin>
               <font>
                  <Font name="System Bold" size="22.0" />
               </font>
            </Button>
         </children>
      </HBox>
   </children>
</VBox>


COMMENTS

Name

CSS,3,HTML,3,Java,59,JavaFX,59,JFoenix,15,PHP,4,Scene Builder,29,SQL,9,
ltr
item
Code Amir: JavaFX Scene Builder Calculator JavaFX
JavaFX Scene Builder Calculator JavaFX
https://i.ytimg.com/vi/n43ksPiJ0mg/0.jpg
https://i.ytimg.com/vi/n43ksPiJ0mg/0.jpg
Code Amir
https://codebyamir.blogspot.com/2020/04/javafx-scene-builder-calculator-javafx.html
https://codebyamir.blogspot.com/
https://codebyamir.blogspot.com/
https://codebyamir.blogspot.com/2020/04/javafx-scene-builder-calculator-javafx.html
true
405304041427069011
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy