Aufgabe 2+3 ausgeführt.

This commit is contained in:
romanschenk37 2022-03-29 20:56:05 +02:00
parent 0488d50f79
commit 70dcb3fc71
2 changed files with 162 additions and 6 deletions

View File

@ -6,6 +6,14 @@ plugins {
id 'java' id 'java'
// Apply the application plugin to add support for building a CLI application. // Apply the application plugin to add support for building a CLI application.
id 'application' id 'application'
// Adding JavaFX support and dependencies
id 'org.openjfx.javafxplugin' version '0.0.12'
}
// Configuration for JavaFX plugin
javafx {
version = '17'
modules = [ 'javafx.controls', 'javafx.fxml' ]
} }
// Project/Module information // Project/Module information

View File

@ -3,8 +3,6 @@ package ch.zhaw.prog2.calculator;
import javafx.application.Application; import javafx.application.Application;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent; import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.geometry.Insets; import javafx.geometry.Insets;
@ -17,8 +15,28 @@ import javafx.scene.paint.Color;
import javafx.stage.Stage; import javafx.stage.Stage;
public class Main extends Application { public class Main extends Application {
private static int VERTICAL_GAP = 5;
private static int HORIZONTAL_GAP = 10;
private Stage primaryStage; private Stage primaryStage;
private CheckMenuItem clearInitialAmount = new CheckMenuItem("Initial amount");
private CheckMenuItem clearReturnInPercent = new CheckMenuItem("Return in %");
private CheckMenuItem clearAnnualCosts = new CheckMenuItem("Annual Costs");
private CheckMenuItem clearNumberOfYears = new CheckMenuItem("Number of years");
private TextField initialAmount = new TextField();
private TextField returnInPercent = new TextField();
private TextField annualCost = new TextField();
private TextField numberOfYears = new TextField();
private TextArea results = new TextArea();
private static final String INFO = """
Enter valid values to
- Initial amount (> 0)
- Return in % (can be +/- or 0)
- Annual Costs (> 0)
- Number of years (> 0)
Calculate displays the annual balance development!";
""";
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);
@ -37,8 +55,12 @@ public class Main extends Application {
private void mainWindow() { private void mainWindow() {
try { try {
BorderPane rootPane = new BorderPane(); BorderPane rootPane = new BorderPane();
//BorderPane top createMenu(rootPane);
MenuBar menuBar = new MenuBar();
createInputOutputPanel(rootPane);
createButtons(rootPane);
// Create scene with root node with size // Create scene with root node with size
Scene scene = new Scene(rootPane, 600, 400); Scene scene = new Scene(rootPane, 600, 400);
@ -55,6 +77,132 @@ public class Main extends Application {
} }
} }
private void createButtons(BorderPane rootPane) {
HBox buttons = new HBox(HORIZONTAL_GAP);
buttons.setAlignment(Pos.BASELINE_CENTER);
Button closeButton = new Button("Close");
Button calculateButton = new Button("Calculate");
closeButton.setOnAction(e -> Platform.exit());
calculateButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
ValueHandler valueHandler = new ValueHandler();
valueHandler.checkValuesAndCalculateResult(initialAmount.getText(), returnInPercent.getText(), annualCost.getText(), numberOfYears.getText());
String result = valueHandler.getResultBound();
if(valueHandler.areValuesOk()){
showResult(result, true, Color.GREEN);
} else {
showResult(result, false, Color.RED);
}
}
});
buttons.getChildren().addAll(calculateButton, closeButton);
buttons.setPadding(new Insets(VERTICAL_GAP, HORIZONTAL_GAP, VERTICAL_GAP, HORIZONTAL_GAP));
rootPane.setBottom(buttons);
}
private void createInputOutputPanel(BorderPane rootPane) {
VBox inputOutputPanel = new VBox(VERTICAL_GAP);
GridPane inputPanel = new GridPane();
VBox resultRows = new VBox();
inputOutputPanel.getChildren().add(inputPanel);
inputOutputPanel.getChildren().add(resultRows);
createInputPanel(inputPanel);
resultRows.getChildren().add(new Label("Results:"));
resultRows.getChildren().add(results);
resultRows.setPadding(new Insets(VERTICAL_GAP, HORIZONTAL_GAP, VERTICAL_GAP, HORIZONTAL_GAP));
rootPane.setCenter(inputOutputPanel);
}
private void createInputPanel(GridPane inputPanel) {
inputPanel.setVgap(5);
inputPanel.setHgap(5);
inputPanel.add(new Label("Initial amount"), 0, 1);
inputPanel.add(new Label("Return rate in %"), 0, 2);
inputPanel.add(new Label("Annual cost"), 0, 3);
inputPanel.add(new Label("Number of years"), 0, 4);
inputPanel.add(initialAmount, 1, 1);
inputPanel.add(returnInPercent, 1, 2);
inputPanel.add(annualCost, 1, 3);
inputPanel.add(numberOfYears, 1, 4);
inputPanel.setPadding(new Insets(VERTICAL_GAP, HORIZONTAL_GAP, VERTICAL_GAP, HORIZONTAL_GAP));
}
private void createMenu(BorderPane rootPane) {
//BorderPane top
MenuBar menuBar = new MenuBar();
Menu clearMenu = new Menu("Clear");
Menu helpMenu = new Menu("?");
// Create MenuItems
MenuItem clearValues = new MenuItem("Clear values");
clearValues.setId("clearValues");
MenuItem clearResults = new MenuItem("Clear results");
clearResults.setId("clearResults");
MenuItem helpShowText = new MenuItem("Show help");
clearMenu.getItems().addAll(clearInitialAmount, clearReturnInPercent, clearAnnualCosts, clearNumberOfYears);
clearMenu.getItems().addAll(new SeparatorMenuItem(), clearValues, new SeparatorMenuItem(), clearResults);
helpMenu.getItems().add(helpShowText);
menuBar.getMenus().addAll(clearMenu, helpMenu);
//using an inner class
ClearHandler clearHandler = new ClearHandler();
clearValues.addEventHandler(ActionEvent.ACTION, clearHandler);
clearResults.addEventHandler(ActionEvent.ACTION, clearHandler);
helpShowText.setAccelerator(KeyCombination.keyCombination("F1"));
helpShowText.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
showResult(INFO, true, Color.BLUE);
}
});
rootPane.setTop(menuBar);
}
/*
* Handler to clear the controls
*/
private class ClearHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
switch (((MenuItem) event.getSource()).getId()) {
case "clearValues" -> {
if (clearInitialAmount.isSelected()) {
initialAmount.clear();
}
if (clearAnnualCosts.isSelected()) {
annualCost.clear();
}
if (clearNumberOfYears.isSelected()) {
numberOfYears.clear();
}
if (clearReturnInPercent.isSelected()) {
returnInPercent.clear();
}
}
case "clearResults" -> results.clear();
}
}
}
private void showResult(String text, boolean clearFirst, Color backColor){
if(clearFirst) {
results.setText(text);
}else{
results.appendText("\n" + text);
}
results.setBorder(new Border(new BorderStroke(backColor, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(1))));
}
} }