import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.stage.Stage; import javafx.scene.control.TextField; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.text.Font; import javafx.event.ActionEvent; /** * Use this template to create Apps with Graphical User Interfaces. * * @author sam.scott1 */ public class BankApp extends Application { // TODO: Instance Variables for View Components and Model Label display; TextField inputField; Button depositButton; Button withdrawButton; BankAccount account; Label comments; // TODO: Private Event Handlers and Helper Methods /** * This is where you create your components and the model and add event * handlers. * * @param stage The main stage * @throws Exception */ public void depositHandler(ActionEvent e) { // Get input from GUI String input = inputField.getText(); // getting input from GUI double amount = Double.parseDouble(input); // parse string to double account.deposit(amount); // Set output in GUI display.setText("Balance: $" + String.format("%.2f",this.account.getBalance()) ); inputField.setText("0.00"); } public void withdrawHandler(ActionEvent e) { // Get input from GUI String input = inputField.getText(); // getting input from GUI double amount = Double.parseDouble(input); // parse string to double if (amount <= account.getBalance()){ account.withdraw(amount); } // Set output in GUI display.setText("Balance: $" + String.format("%.2f",account.getBalance()) ); inputField.setText("0.00"); } @Override public void start(Stage stage) throws Exception { Pane root = new Pane(); Scene scene = new Scene(root, 400, 225); // set the size here stage.setTitle("Dave's Account"); // set the window title here stage.setScene(scene); // TODO: Add your GUI-building code here // 1. Create the model account = new BankAccount("Dave" ); // 2. Create the GUI components depositButton = new Button ("Deposit"); withdrawButton = new Button ("Withdraw"); inputField = new TextField("0.00"); display = new Label("Balance: $"+ String.format("%.2f",account.getBalance()) ); // 3. Add components to the root root.getChildren().addAll(depositButton, withdrawButton,inputField,display); // 4. Configure the components (colors, fonts, size, location) display.relocate(25,25); display.setFont(new Font("System",40)); display.setStyle("-fx-text-fill: green;"); depositButton.relocate(200,100); withdrawButton.relocate(300,100); inputField.relocate(25,100); inputField.setPrefWidth(120); // 5. Add Event Handlers and do final setup depositButton.setOnAction( this::depositHandler ); withdrawButton.setOnAction( this::withdrawHandler ); // 6. Show the stage stage.show(); } /** * Make no changes here. * * @param args unused */ public static void main(String[] args) { launch(args); } }