import javafx.scene.control.*; import javafx.scene.text.Font; import javafx.event.ActionEvent; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.stage.Stage; import javafx.scene.image.Image; import javafx.scene.image.ImageView; /** * Use this template to create Apps with Graphical User Interfaces. * * @author Dave Slemon * @version v101 */ public class PiggyApp extends Application { // TODO: Instance Variables for View Components and Model private Label appTitle; private TextField inputBox; private Button nickelButton; private Button dimeButton; private Button quarterButton; private Label footer; private Piggy pig; // TODO: Private Event Handlers and Helper Methods private void nickelRoutine(ActionEvent e) { Coin n = new Coin("NICKEL"); pig.addCoin(n); inputBox.setText(String.format("%.2f",pig.getTotal())); footer.setText(pig.toString()); } private void dimeRoutine(ActionEvent e) { Coin n = new Coin("DIME"); pig.addCoin(n); inputBox.setText(String.format("%.2f",pig.getTotal())); footer.setText(pig.toString()); } private void quarterRoutine(ActionEvent e) { Coin n = new Coin("QUARTER"); pig.addCoin(n); inputBox.setText(String.format("%.2f",pig.getTotal())); footer.setText(pig.toString()); } /** * This is where you create your components and the model and add event * handlers. * * @param stage The main stage * @throws Exception */ @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 Piggy Bank"); // set the window title here stage.setScene(scene); // TODO: Add your GUI-building code here scene.getStylesheets().add("myStyles.css"); Image image = new Image("piggy.png"); ImageView imageView = new ImageView(image); imageView.setX(250); imageView.setY(20); imageView.setFitHeight(90); //imageView.setFitWidth(100); imageView.setPreserveRatio(true); // 1. Create the model pig = new Piggy("Dave"); // 2. Create the GUI components appTitle = new Label("Piggy Bank"); inputBox = new TextField("0.00"); nickelButton = new Button ("Nickel"); dimeButton = new Button ("Dime"); quarterButton = new Button("Quarter"); footer = new Label(""); // 3. Add components to the root root.getChildren().addAll(appTitle,inputBox,nickelButton, dimeButton,quarterButton,footer,imageView); // 4. Configure the components (colors, fonts, size, location) appTitle.relocate(90,20); //appTitle.setFont( new Font("System",40)); appTitle.setId("appTitle"); inputBox.relocate(130,90); inputBox.setPrefHeight(50); inputBox.setPrefWidth(120); inputBox.setFont( new Font("System",25)); inputBox.setEditable(false); nickelButton.relocate(60,160); dimeButton.relocate(160,160); quarterButton.relocate(260,160); footer.relocate(10,200); footer.setId("footer"); // 5. Add Event Handlers and do final setup nickelButton.setOnAction(this::nickelRoutine); dimeButton.setOnAction(this::dimeRoutine); quarterButton.setOnAction(this::quarterRoutine); // 6. Show the stage stage.show(); } /** * Make no changes here. * * @param args unused */ public static void main(String[] args) { launch(args); } }