{"id":639,"date":"2020-01-28T09:23:40","date_gmt":"2020-01-28T09:23:40","guid":{"rendered":"http:\/\/iludis.de\/?p=639"},"modified":"2022-04-20T10:08:51","modified_gmt":"2022-04-20T10:08:51","slug":"sourcecode-taschenrechner-app-javafx","status":"publish","type":"page","link":"https:\/\/iludis.de\/?page_id=639","title":{"rendered":"Sourcecode Taschenrechner-App JavaFX"},"content":{"rendered":"<p>Main:<\/p>\n<pre>package sample;\r\n\r\nimport javafx.application.Application;\r\nimport javafx.fxml.FXMLLoader;\r\nimport javafx.scene.Parent;\r\nimport javafx.scene.Scene;\r\nimport javafx.stage.Stage;\r\n\r\npublic class Main extends Application {\r\n\r\n    @Override\r\n    public void start(Stage primaryStage) throws Exception{\r\n        Parent root = FXMLLoader.load(getClass().getResource(&quot;sample.fxml&quot;));\r\n        primaryStage.setTitle(&quot;Calc&quot;);\r\n        primaryStage.setScene(new Scene(root, 190, 250));\r\n        primaryStage.show();\r\n    }\r\n\r\n\r\n    public static void main(String[] args) {\r\n        launch(args);\r\n    }\r\n}\r\n\r\n<\/pre>\n<p>Model:<\/p>\n<pre>package sample;\r\n\r\npublic class Model {\r\n    int zahl=0;\r\n    int speicherzahl = 0;\r\n    String funktion = &quot;&quot;;\r\n\r\n    public Model(){\r\n        System.out.println(&quot;Model initialisiert&quot;);\r\n    }\r\n    private void setZahl(int neueZahl){\r\n        this.zahl = neueZahl;\r\n    }\r\n    private void setSpeicherzahl(int neueZahl){\r\n        this.speicherzahl = neueZahl;\r\n    }\r\n    public int getZahl(){\r\n        return this.zahl;\r\n    }\r\n    public int getSpeicherzahl(){\r\n        return this.speicherzahl;\r\n    }\r\n    public void zahleingabe(int number) {\r\n        if (this.zahl&gt;0) {\r\n            setZahl(zahl * 10 + number);\r\n        }\r\n        if (this.zahl == 0){\r\n            setZahl(number);\r\n        }\r\n    }\r\n    public void zahlruecknahme() {\r\n        setZahl(this.zahl \/ 10);\r\n        if(this.zahl==0){\r\n            this.funktion=&quot;&quot;;\r\n        }\r\n    }\r\n\r\n    public void machefunktion(String fkt) {\r\n        if (!fkt.equals(&quot;&quot;)) {\r\n            this.funktion = fkt;\r\n            setSpeicherzahl(getZahl());\r\n            setZahl(0);\r\n            System.out.println(&quot;funktion kapiert!&quot;);\r\n        }\r\n        if (fkt.equals(&quot;&quot;)) {\r\n            this.funktion = &quot;&quot;;\r\n            setSpeicherzahl(0);\r\n        }\r\n    }\r\n\r\n    public void berechneErgebnis(){\r\n        switch (this.funktion){\r\n            case &quot;plus&quot;:\r\n                setZahl(getZahl() + getSpeicherzahl());\r\n                break;\r\n            case &quot;minus&quot;:\r\n                setZahl(getZahl() - getSpeicherzahl());\r\n                break;\r\n            case &quot;mal&quot;:\r\n                setZahl(getZahl() * getSpeicherzahl());\r\n                break;\r\n            case &quot;geteilt&quot;:\r\n                setZahl(getZahl() \/ getSpeicherzahl());\r\n                break;\r\n        }\r\n    }\r\n}<\/pre>\n<p>Controller:<\/p>\n<pre>package sample;\r\nimport javafx.event.ActionEvent;\r\nimport javafx.fxml.FXML;\r\nimport javafx.scene.control.Button;\r\nimport javafx.scene.control.Label;\r\nimport javafx.scene.control.TextField;\r\n\r\npublic class Controller {\r\n    Model tmodell = new Model();\r\n\r\n    @FXML\r\n    private Button plus;\r\n\r\n    @FXML\r\n    private Button minus;\r\n\r\n    @FXML\r\n    private Button mal;\r\n\r\n    @FXML\r\n    private Button geteilt;\r\n\r\n    @FXML\r\n    private Button b1;\r\n\r\n    @FXML\r\n    private Button b2;\r\n\r\n    @FXML\r\n    private Button b3;\r\n\r\n    @FXML\r\n    private Button b4;\r\n\r\n    @FXML\r\n    private Button b5;\r\n\r\n    @FXML\r\n    private Button b6;\r\n\r\n    @FXML\r\n    private Button b7;\r\n\r\n    @FXML\r\n    private Button b8;\r\n\r\n    @FXML\r\n    private Button b9;\r\n\r\n    @FXML\r\n    private Button b0;\r\n\r\n    @FXML\r\n    private Button ce;\r\n\r\n    @FXML\r\n    private Button istgleichbutton;\r\n\r\n    @FXML\r\n    private TextField resultWindow;\r\n\r\n    @FXML\r\n    private Label opWindow;\r\n\r\n    @FXML\r\n    void istgleich(ActionEvent event) {\r\n        opWindow.setText(opWindow.getText() + &quot; &quot; + tmodell.getZahl() + &quot; =&quot;);\r\n        tmodell.berechneErgebnis();\r\n        zahlausgabe();\r\n    }\r\n\r\n    @FXML\r\n    void funktionButton(ActionEvent event) {\r\n        Button Gedrueckt = (Button) event.getSource();\r\n        System.out.println(Gedrueckt);\r\n        String Buttonschrift = Gedrueckt.getText();\r\n        System.out.println(Buttonschrift);\r\n        String welcherButton = Gedrueckt.getId();\r\n        System.out.println(welcherButton);\r\n        tmodell.machefunktion(welcherButton);\r\n        opWindow.setText(String.valueOf(tmodell.getSpeicherzahl()) + &quot; &quot; + Buttonschrift);\r\n    }\r\n\r\n    @FXML\r\n    void reset(ActionEvent event) {\r\n        \r\n        tmodell.zahlruecknahme();\r\n        zahlausgabe();\r\n    }\r\n\r\n    @FXML\r\n    void zahlEingeben(ActionEvent event) {\r\n        Button Gedrueckt = (Button) event.getSource();\r\n        String welcherButton = Gedrueckt.getId();\r\n        int number = Integer.valueOf(welcherButton.substring(1,2));\r\n        tmodell.zahleingabe(number);\r\n        zahlausgabe();\r\n    }\r\n\r\n    void zahlausgabe(){\r\n        resultWindow.setText(String.valueOf(tmodell.getZahl()));\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Sample.fxml:<\/p>\n<pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n\r\n&lt;?import javafx.geometry.Point3D?&gt;\r\n&lt;?import javafx.scene.control.Button?&gt;\r\n&lt;?import javafx.scene.control.Label?&gt;\r\n&lt;?import javafx.scene.control.TextField?&gt;\r\n&lt;?import javafx.scene.layout.AnchorPane?&gt;\r\n&lt;?import javafx.scene.layout.ColumnConstraints?&gt;\r\n&lt;?import javafx.scene.layout.GridPane?&gt;\r\n&lt;?import javafx.scene.layout.RowConstraints?&gt;\r\n\r\n&lt;GridPane alignment=&quot;center&quot; hgap=&quot;10&quot; vgap=&quot;10&quot; xmlns=&quot;http:\/\/javafx.com\/javafx\/11.0.1&quot; xmlns:fx=&quot;http:\/\/javafx.com\/fxml\/1&quot; fx:controller=&quot;sample.Controller&quot;&gt;\r\n   &lt;columnConstraints&gt;\r\n      &lt;ColumnConstraints \/&gt;\r\n   &lt;\/columnConstraints&gt;\r\n   &lt;rowConstraints&gt;\r\n      &lt;RowConstraints \/&gt;\r\n   &lt;\/rowConstraints&gt;\r\n   &lt;children&gt;\r\n      &lt;AnchorPane prefHeight=&quot;250.0&quot; prefWidth=&quot;190.0&quot;&gt;\r\n         &lt;children&gt;\r\n            &lt;Button fx:id=&quot;plus&quot; layoutX=&quot;20.0&quot; layoutY=&quot;78.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#funktionButton&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;+&quot; \/&gt;\r\n            &lt;Button fx:id=&quot;minus&quot; layoutX=&quot;59.0&quot; layoutY=&quot;78.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#funktionButton&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;-&quot; \/&gt;\r\n            &lt;Button fx:id=&quot;mal&quot; layoutX=&quot;99.0&quot; layoutY=&quot;78.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#funktionButton&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;*&quot; \/&gt;\r\n            &lt;Button fx:id=&quot;geteilt&quot; layoutX=&quot;139.0&quot; layoutY=&quot;78.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#funktionButton&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;:&quot; \/&gt;\r\n            &lt;Button fx:id=&quot;b1&quot; layoutX=&quot;20.0&quot; layoutY=&quot;118.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#zahlEingeben&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;1&quot; \/&gt;\r\n            &lt;Button fx:id=&quot;b2&quot; layoutX=&quot;59.0&quot; layoutY=&quot;118.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#zahlEingeben&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;2&quot; \/&gt;\r\n            &lt;Button fx:id=&quot;b3&quot; layoutX=&quot;99.0&quot; layoutY=&quot;118.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#zahlEingeben&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;3&quot; \/&gt;\r\n            &lt;Button fx:id=&quot;b4&quot; layoutX=&quot;139.0&quot; layoutY=&quot;118.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#zahlEingeben&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;4&quot; \/&gt;\r\n            &lt;Button fx:id=&quot;b5&quot; layoutX=&quot;20.0&quot; layoutY=&quot;158.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#zahlEingeben&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;5&quot;&gt;\r\n               &lt;rotationAxis&gt;\r\n                  &lt;Point3D x=&quot;-3.0&quot; z=&quot;1.0&quot; \/&gt;\r\n               &lt;\/rotationAxis&gt;\r\n            &lt;\/Button&gt;\r\n            &lt;Button fx:id=&quot;b6&quot; layoutX=&quot;59.0&quot; layoutY=&quot;158.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#zahlEingeben&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;6&quot;&gt;\r\n               &lt;rotationAxis&gt;\r\n                  &lt;Point3D x=&quot;-3.0&quot; z=&quot;1.0&quot; \/&gt;\r\n               &lt;\/rotationAxis&gt;\r\n            &lt;\/Button&gt;\r\n            &lt;Button fx:id=&quot;b7&quot; layoutX=&quot;99.0&quot; layoutY=&quot;158.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#zahlEingeben&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;7&quot;&gt;\r\n               &lt;rotationAxis&gt;\r\n                  &lt;Point3D x=&quot;-3.0&quot; z=&quot;1.0&quot; \/&gt;\r\n               &lt;\/rotationAxis&gt;\r\n            &lt;\/Button&gt;\r\n            &lt;Button fx:id=&quot;b8&quot; layoutX=&quot;139.0&quot; layoutY=&quot;158.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#zahlEingeben&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;8&quot;&gt;\r\n               &lt;rotationAxis&gt;\r\n                  &lt;Point3D x=&quot;-3.0&quot; z=&quot;1.0&quot; \/&gt;\r\n               &lt;\/rotationAxis&gt;\r\n            &lt;\/Button&gt;\r\n            &lt;Button fx:id=&quot;b9&quot; layoutX=&quot;20.0&quot; layoutY=&quot;198.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#zahlEingeben&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;9&quot; \/&gt;\r\n            &lt;Button fx:id=&quot;b0&quot; layoutX=&quot;59.0&quot; layoutY=&quot;198.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#zahlEingeben&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;0&quot; \/&gt;\r\n            &lt;Button fx:id=&quot;ce&quot; layoutX=&quot;99.0&quot; layoutY=&quot;198.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#reset&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;ce&quot; \/&gt;\r\n            &lt;Button fx:id=&quot;istgleichbutton&quot; layoutX=&quot;139.0&quot; layoutY=&quot;198.0&quot; maxHeight=&quot;30.0&quot; maxWidth=&quot;30.0&quot; minHeight=&quot;30.0&quot; minWidth=&quot;30.0&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#istgleich&quot; prefHeight=&quot;30.0&quot; prefWidth=&quot;30.0&quot; text=&quot;=&quot; \/&gt;\r\n            &lt;TextField fx:id=&quot;resultWindow&quot; alignment=&quot;CENTER_RIGHT&quot; layoutX=&quot;21.0&quot; layoutY=&quot;43.0&quot; promptText=&quot;0&quot; \/&gt;\r\n            &lt;Label fx:id=&quot;opWindow&quot; alignment=&quot;CENTER_RIGHT&quot; contentDisplay=&quot;RIGHT&quot; layoutX=&quot;21.0&quot; layoutY=&quot;20.0&quot; prefHeight=&quot;17.0&quot; prefWidth=&quot;149.0&quot; text=&quot; &quot; \/&gt;\r\n         &lt;\/children&gt;\r\n      &lt;\/AnchorPane&gt;\r\n   &lt;\/children&gt;\r\n&lt;\/GridPane&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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(&quot;sample.fxml&quot;)); primaryStage.setTitle(&quot;Calc&quot;); 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;&hellip; <\/p>\n<p class=\"toivo-read-more\"><a href=\"https:\/\/iludis.de\/?page_id=639\" class=\"more-link\">Read more <span class=\"screen-reader-text\">Sourcecode Taschenrechner-App JavaFX<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":{"0":"post-639","1":"page","2":"type-page","3":"status-publish","5":"entry"},"_links":{"self":[{"href":"https:\/\/iludis.de\/index.php?rest_route=\/wp\/v2\/pages\/639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iludis.de\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/iludis.de\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/iludis.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/iludis.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=639"}],"version-history":[{"count":0,"href":"https:\/\/iludis.de\/index.php?rest_route=\/wp\/v2\/pages\/639\/revisions"}],"wp:attachment":[{"href":"https:\/\/iludis.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}