JavaFX-Taschenrechner Florian S.

Main.java:

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("Taschenrechner");
primaryStage.setScene(new Scene(root, 230, 280));
primaryStage.show();
}

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

 


Controller.java:

package sample;

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

public class Controller {
private Model tmodel = new Model();
@FXML
private Button ButtonPlus;
@FXML
private Button ButtonMinus;
@FXML
private Button ButtonMal;
@FXML
private Button ButtonDurch;
@FXML
private Button ButtonEins;
@FXML
private Button ButtonZwei;
@FXML
private Button ButtonDrei;
@FXML
private Button ButtonVier;
@FXML
private Button ButtonFuenf;
@FXML
private Button ButtonSechs;
@FXML
private Button ButtonSieben;
@FXML
private Button ButtonAcht;
@FXML
private Button ButtonNeun;
@FXML
private Button ButtonNull;
@FXML
private Button ButtonGleich;
@FXML
private Button ButtonCE;
@FXML
private Button ButtonCLX;
@FXML
private Label Label1;

public void ButtonPressed(ActionEvent event) {
Button pressedButton = (Button) event.getSource();
String ButtonTag = pressedButton.getText();
int int1 = Integer.valueOf(Label1.getText());
if (String.valueOf(tmodel.getVar1()).equals("0")) {
tmodel.setVar1(int1);
System.out.println("var1 gesetzt:");
System.out.println(tmodel.getVar1());
}
else {
tmodel.setVar2(Integer.valueOf(Label1.getText()));
System.out.println("var2 gesetzt:");
System.out.println(tmodel.getVar2());
tmodel.ForceExecute(ButtonTag);
}
Label1.setText("0");
if (ButtonTag.equals(ButtonPlus.getText())) {
tmodel.setStr1("+");
} else if (ButtonTag.equals(ButtonMinus.getText())) {
tmodel.setStr1("-");
} else if (ButtonTag.equals(ButtonMal.getText())) {
tmodel.setStr1("*");
} else if (ButtonTag.equals(ButtonDurch.getText())) {
tmodel.setStr1("/");
}
Label1.setText("0");
}
public void NumPressed(ActionEvent event) {
Button pressedButton = (Button) event.getSource();
String ButtonTag = pressedButton.getText();
int oldNum = Integer.valueOf(Label1.getText());
int newNum = tmodel.CalcNewNum(oldNum, Integer.valueOf(ButtonTag));
Label1.setText(String.valueOf(newNum));
}
public void SpecialButton(ActionEvent event) {
int ergebnis = 0;
Button pressedButton = (Button) event.getSource();
String ButtonTag = pressedButton.getText();
if (ButtonTag.equals("CE")) {
Label1.setText(String.valueOf(Integer.valueOf(Label1.getText())/10));
}
else if (ButtonTag.equals("=")) {
System.out.println("ausrechnen");
tmodel.setVar2(Integer.valueOf(Label1.getText()));
System.out.println("var2 gesetzt:");
System.out.println(tmodel.getVar2());
if (tmodel.getStr1().equals("+")) {
ergebnis = tmodel.Addieren();
} else if (tmodel.getStr1().equals("-")) {
ergebnis = tmodel.Subtrahieren();
} else if (tmodel.getStr1().equals("*")) {
ergebnis = tmodel.Multiplizieren();
} else if (tmodel.getStr1().equals("/")) {
ergebnis = tmodel.Dividieren();
}

if (tmodel.getState() == 1) {
ergebnis = tmodel.Ausrechnen(ergebnis);
tmodel.setState(0);
}
System.out.println(ergebnis);
Label1.setText(String.valueOf(ergebnis));
tmodel.setVar1(0);
tmodel.setVar2(0);
tmodel.setVar3(0);
tmodel.setStr1("");
tmodel.setStr2("");
tmodel.setStr3("");
}
else if (ButtonTag.equals("CLX")) {
Label1.setText("0");
tmodel.setVar1(0);
tmodel.setVar2(0);
tmodel.setVar3(0);
tmodel.setStr1("");
tmodel.setStr2("");
tmodel.setStr3("");
tmodel.setState(0);
}
}
}

 


Model.java:

package sample;

public class Model {
private int var1 = 0;
private int var2 = 0;
private int var3 = 0;
private String str1 = "";
private String str2 = "";
private String str3 = "";
private int state = 0;
public int Addieren() {
System.out.println("addieren");
return var1 + var2;
}
public int Subtrahieren() {
System.out.println("subtrahieren");
return var1 - var2;
}
public int Multiplizieren() {
System.out.println("multiplizieren");
return var1 * var2;
}
public int Dividieren() {
System.out.println("dividieren");
return var1 / var2;
}
public int getVar1() {
return var1;
}
public void setVar1(int int1) {
var1 = int1;
}
public int getVar2() {
return var2;
}
public void setVar2(int int1) {
var2 = int1;
}
public int getVar3() {
return var3;
}
public void setVar3(int int1){
var3 = int1;
}
public String getStr1() {
return str1;
}
public void setStr1(String str) {
str1 = str;
}
public String getStr2() {
return str2;
}
public void setStr2(String str){
str2 = str;
}
public String getStr3() {
return str3;
}
public void setStr3(String str){
str3 = str;
}
public int getState() {
return state;
}
public void setState(int int1) {
state = int1;
}
public int CalcNewNum (int oldNum, int preNum) {
return oldNum*10+preNum;
}
public void ForceExecute (String ButtonTag) {
if (ButtonTag.equals("+") || ButtonTag.equals("-")) {
if (str1.equals("+")) {
var1 = Addieren();
} else if (str1.equals("-")) {
var1 = Subtrahieren();
} else if (str1.equals("*")) {
var1 = Multiplizieren();
} else if (str1.equals("/")) {
var1 = Dividieren();
}
if (state == 1) {
if (str3.equals("+")) {
var1 = var3 + var1;
}
else if (str3.equals("-")) {
var1 = var3 - var2;
}
state = 0;
}
}
else if (ButtonTag.equals("*") || ButtonTag.equals("/")) {
if (str1.equals("*")) {
var1 = Multiplizieren();
}
else if (str1.equals("/")) {
var1 = Dividieren();
}
else if (state == 0) {
state = 1;
var3 = var1;
var1 = var2;
var2 = 0;
str3 = str1;
}
else if (state == 1) {
if (str2.equals("+")) {
var1 = Addieren();
} else if (str2.equals("-")) {
var1 = Subtrahieren();
} else if (str2.equals("*")) {
var1 = Multiplizieren();
} else if (str2.equals("/")) {
var1 = Dividieren();
}
}
}
str2 = ButtonTag;

}
public int Ausrechnen (int int1) {
int ergebnis = 0;
if (str3.equals("+")) {
ergebnis = var3 + int1;
}
else if ( str3.equals("-")) {
ergebnis = var3 - int1;
}
return ergebnis;
}
}

 


Sample.fxml:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?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 />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<Pane prefHeight="280.0" prefWidth="230.0">
<children>
<Label fx:id="Label1" layoutX="64.0" layoutY="28.0" prefHeight="17.0" prefWidth="209.0" text="0" />
<Button fx:id="ButtonPlus" layoutX="201.0" layoutY="73.0" mnemonicParsing="false" onAction="#ButtonPressed" text="+" />
<Button fx:id="ButtonMinus" layoutX="202.0" layoutY="123.0" mnemonicParsing="false" onAction="#ButtonPressed" text="-" />
<Button fx:id="ButtonMal" layoutX="201.0" layoutY="173.0" mnemonicParsing="false" onAction="#ButtonPressed" text="*" />
<Button fx:id="ButtonDurch" layoutX="201.0" layoutY="223.0" mnemonicParsing="false" onAction="#ButtonPressed" text="/" />
<Button fx:id="ButtonDrei" layoutX="157.0" layoutY="73.0" mnemonicParsing="false" onAction="#NumPressed" text="3" />
<Button fx:id="ButtonZwei" layoutX="113.0" layoutY="73.0" mnemonicParsing="false" onAction="#NumPressed" text="2" />
<Button fx:id="ButtonEins" layoutX="68.0" layoutY="73.0" mnemonicParsing="false" onAction="#NumPressed" text="1" />
<Button fx:id="ButtonVier" layoutX="67.0" layoutY="123.0" mnemonicParsing="false" onAction="#NumPressed" text="4" />
<Button fx:id="ButtonFuenf" layoutX="113.0" layoutY="123.0" mnemonicParsing="false" onAction="#NumPressed" text="5" />
<Button fx:id="ButtonSechs" layoutX="157.0" layoutY="123.0" mnemonicParsing="false" onAction="#NumPressed" text="6" />
<Button fx:id="ButtonSieben" layoutX="68.0" layoutY="173.0" mnemonicParsing="false" onAction="#NumPressed" text="7" />
<Button fx:id="ButtonAcht" layoutX="113.0" layoutY="173.0" mnemonicParsing="false" onAction="#NumPressed" text="8" />
<Button fx:id="ButtonNeun" layoutX="157.0" layoutY="173.0" mnemonicParsing="false" onAction="#NumPressed" text="9" />
<Button fx:id="ButtonCE" layoutX="64.0" layoutY="223.0" mnemonicParsing="false" onAction="#SpecialButton" text="CE" />
<Button fx:id="ButtonNull" layoutX="113.0" layoutY="223.0" mnemonicParsing="false" onAction="#NumPressed" text="0" />
<Button fx:id="ButtonGleich" layoutX="156.0" layoutY="223.0" mnemonicParsing="false" onAction="#SpecialButton" text="=" />
<Button fx:id="ButtonCLX" layoutX="14.0" layoutY="223.0" mnemonicParsing="false" onAction="#SpecialButton" text="CLX" />
</children>
</Pane>
</children>
</GridPane>

 


Vielen Dank an Florian für das zur Verfügung stellen des Codes! 🙂