Sourcecode Taschenrechner-App JavaFX

Main:

package sample;

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

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Calc");
        primaryStage.setScene(new Scene(root, 190, 250));
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Model:

package sample;

public class Model {
    int zahl=0;
    int speicherzahl = 0;
    String funktion = "";

    public Model(){
        System.out.println("Model initialisiert");
    }
    private void setZahl(int neueZahl){
        this.zahl = neueZahl;
    }
    private void setSpeicherzahl(int neueZahl){
        this.speicherzahl = neueZahl;
    }
    public int getZahl(){
        return this.zahl;
    }
    public int getSpeicherzahl(){
        return this.speicherzahl;
    }
    public void zahleingabe(int number) {
        if (this.zahl>0) {
            setZahl(zahl * 10 + number);
        }
        if (this.zahl == 0){
            setZahl(number);
        }
    }
    public void zahlruecknahme() {
        setZahl(this.zahl / 10);
        if(this.zahl==0){
            this.funktion="";
        }
    }

    public void machefunktion(String fkt) {
        if (!fkt.equals("")) {
            this.funktion = fkt;
            setSpeicherzahl(getZahl());
            setZahl(0);
            System.out.println("funktion kapiert!");
        }
        if (fkt.equals("")) {
            this.funktion = "";
            setSpeicherzahl(0);
        }
    }

    public void berechneErgebnis(){
        switch (this.funktion){
            case "plus":
                setZahl(getZahl() + getSpeicherzahl());
                break;
            case "minus":
                setZahl(getZahl() - getSpeicherzahl());
                break;
            case "mal":
                setZahl(getZahl() * getSpeicherzahl());
                break;
            case "geteilt":
                setZahl(getZahl() / getSpeicherzahl());
                break;
        }
    }
}

Controller:

package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class Controller {
    Model tmodell = new Model();

    @FXML
    private Button plus;

    @FXML
    private Button minus;

    @FXML
    private Button mal;

    @FXML
    private Button geteilt;

    @FXML
    private Button b1;

    @FXML
    private Button b2;

    @FXML
    private Button b3;

    @FXML
    private Button b4;

    @FXML
    private Button b5;

    @FXML
    private Button b6;

    @FXML
    private Button b7;

    @FXML
    private Button b8;

    @FXML
    private Button b9;

    @FXML
    private Button b0;

    @FXML
    private Button ce;

    @FXML
    private Button istgleichbutton;

    @FXML
    private TextField resultWindow;

    @FXML
    private Label opWindow;

    @FXML
    void istgleich(ActionEvent event) {
        opWindow.setText(opWindow.getText() + " " + tmodell.getZahl() + " =");
        tmodell.berechneErgebnis();
        zahlausgabe();
    }

    @FXML
    void funktionButton(ActionEvent event) {
        Button Gedrueckt = (Button) event.getSource();
        System.out.println(Gedrueckt);
        String Buttonschrift = Gedrueckt.getText();
        System.out.println(Buttonschrift);
        String welcherButton = Gedrueckt.getId();
        System.out.println(welcherButton);
        tmodell.machefunktion(welcherButton);
        opWindow.setText(String.valueOf(tmodell.getSpeicherzahl()) + " " + Buttonschrift);
    }

    @FXML
    void reset(ActionEvent event) {
        
        tmodell.zahlruecknahme();
        zahlausgabe();
    }

    @FXML
    void zahlEingeben(ActionEvent event) {
        Button Gedrueckt = (Button) event.getSource();
        String welcherButton = Gedrueckt.getId();
        int number = Integer.valueOf(welcherButton.substring(1,2));
        tmodell.zahleingabe(number);
        zahlausgabe();
    }

    void zahlausgabe(){
        resultWindow.setText(String.valueOf(tmodell.getZahl()));
    }
}

 

Sample.fxml:

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

<?import javafx.geometry.Point3D?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <columnConstraints>
      <ColumnConstraints />
   </columnConstraints>
   <rowConstraints>
      <RowConstraints />
   </rowConstraints>
   <children>
      <AnchorPane prefHeight="250.0" prefWidth="190.0">
         <children>
            <Button fx:id="plus" layoutX="20.0" layoutY="78.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#funktionButton" prefHeight="30.0" prefWidth="30.0" text="+" />
            <Button fx:id="minus" layoutX="59.0" layoutY="78.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#funktionButton" prefHeight="30.0" prefWidth="30.0" text="-" />
            <Button fx:id="mal" layoutX="99.0" layoutY="78.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#funktionButton" prefHeight="30.0" prefWidth="30.0" text="*" />
            <Button fx:id="geteilt" layoutX="139.0" layoutY="78.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#funktionButton" prefHeight="30.0" prefWidth="30.0" text=":" />
            <Button fx:id="b1" layoutX="20.0" layoutY="118.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#zahlEingeben" prefHeight="30.0" prefWidth="30.0" text="1" />
            <Button fx:id="b2" layoutX="59.0" layoutY="118.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#zahlEingeben" prefHeight="30.0" prefWidth="30.0" text="2" />
            <Button fx:id="b3" layoutX="99.0" layoutY="118.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#zahlEingeben" prefHeight="30.0" prefWidth="30.0" text="3" />
            <Button fx:id="b4" layoutX="139.0" layoutY="118.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#zahlEingeben" prefHeight="30.0" prefWidth="30.0" text="4" />
            <Button fx:id="b5" layoutX="20.0" layoutY="158.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#zahlEingeben" prefHeight="30.0" prefWidth="30.0" text="5">
               <rotationAxis>
                  <Point3D x="-3.0" z="1.0" />
               </rotationAxis>
            </Button>
            <Button fx:id="b6" layoutX="59.0" layoutY="158.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#zahlEingeben" prefHeight="30.0" prefWidth="30.0" text="6">
               <rotationAxis>
                  <Point3D x="-3.0" z="1.0" />
               </rotationAxis>
            </Button>
            <Button fx:id="b7" layoutX="99.0" layoutY="158.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#zahlEingeben" prefHeight="30.0" prefWidth="30.0" text="7">
               <rotationAxis>
                  <Point3D x="-3.0" z="1.0" />
               </rotationAxis>
            </Button>
            <Button fx:id="b8" layoutX="139.0" layoutY="158.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#zahlEingeben" prefHeight="30.0" prefWidth="30.0" text="8">
               <rotationAxis>
                  <Point3D x="-3.0" z="1.0" />
               </rotationAxis>
            </Button>
            <Button fx:id="b9" layoutX="20.0" layoutY="198.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#zahlEingeben" prefHeight="30.0" prefWidth="30.0" text="9" />
            <Button fx:id="b0" layoutX="59.0" layoutY="198.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#zahlEingeben" prefHeight="30.0" prefWidth="30.0" text="0" />
            <Button fx:id="ce" layoutX="99.0" layoutY="198.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#reset" prefHeight="30.0" prefWidth="30.0" text="ce" />
            <Button fx:id="istgleichbutton" layoutX="139.0" layoutY="198.0" maxHeight="30.0" maxWidth="30.0" minHeight="30.0" minWidth="30.0" mnemonicParsing="false" onAction="#istgleich" prefHeight="30.0" prefWidth="30.0" text="=" />
            <TextField fx:id="resultWindow" alignment="CENTER_RIGHT" layoutX="21.0" layoutY="43.0" promptText="0" />
            <Label fx:id="opWindow" alignment="CENTER_RIGHT" contentDisplay="RIGHT" layoutX="21.0" layoutY="20.0" prefHeight="17.0" prefWidth="149.0" text=" " />
         </children>
      </AnchorPane>
   </children>
</GridPane>