Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Collin Emond
- * 5-10-16
- * Final Exam
- * CSC-112
- * Prof. Silvestri
- * Phase 1: The GUI
- * Create a GUI that implements error checking for the ZipCode Translator
- * cremond0001@student.stcc.edu
- */
- import javafx.application.Application;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.*;
- import javafx.scene.layout.*;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Line;
- import javafx.stage.Stage;
- public class ZipCodeTranslatorGUI extends Application {
- TextField txtzipcode = new TextField();
- TextField txtcity = new TextField();
- TextField txtstate = new TextField();
- Button zipcityBtn = new Button("Zip To City");
- Button citytozipBtn = new Button("City to Zip");
- TextField txtzipcodes = new TextField("Zip Code(s)");
- @Override
- public void start(Stage primaryStage) {
- // Create a new pane to hold
- BorderPane pane = new BorderPane();
- pane.setTop(topHalf());
- pane.setCenter(middlePart());
- pane.setBottom(bottomHalf());
- zipcityBtn.setOnAction(e -> checkzipcity());
- citytozipBtn.setOnAction(e -> checkcitytozip());
- // Create a scene
- Scene scene = new Scene(pane, 800, 300);
- primaryStage.setTitle("Zip Code to City/State Translator App");
- primaryStage.setScene(scene);
- primaryStage.show();
- primaryStage.setResizable(false);
- }
- // TextStatus Field
- TextField txtstatus = new TextField();
- // Method that executes Zip to City Button Code
- private void checkzipcity() {
- if (txtzipcode.getText().matches("(\\d{5})")) {
- txtstatus.setText("Valid Zip Code");
- } else {
- txtstatus.setText("Not a Valid Zip Code");
- }
- }
- // Method that executes City to Zip Button Code
- private void checkcitytozip() {
- if (txtcity.getText().matches("^[a-zA-Z\\s-]+$") && txtstate.getText().matches("^[A-Za-z]{2}")) {
- txtstatus.setText("Valid City and State");
- } else {
- txtstatus.setText("Not a Valid City and State");
- }
- }
- // Top Half of Program
- public VBox topHalf() {
- HBox hbox = new HBox();
- hbox.setPadding(new Insets(15, 12, 15, 12));
- hbox.setSpacing(15);
- hbox.setAlignment(Pos.CENTER);
- // VBox to hold HBox and Separator
- VBox vbox = new VBox();
- vbox.setAlignment(Pos.CENTER);
- // Add Line
- Line separator = new Line();
- separator.setStrokeWidth(5);
- separator.setStroke(Color.BLACK);
- separator.setEndX(800.0);
- separator.autosize();
- // Add Labels
- Label lblzipcode = new Label("Zip Code:");
- Label lblcity = new Label("City:");
- Label lblstate = new Label("State:");
- // Add Properties of TextField
- txtzipcode.setPrefColumnCount(5);
- txtzipcode.setPrefWidth(80);
- TextField txtcity = new TextField();
- txtcity.setPrefColumnCount(25);
- txtcity.setPrefWidth(80);
- txtcity.setEditable(false);
- txtcity.setMouseTransparent(true);
- txtcity.setFocusTraversable(false);
- TextField txtstate = new TextField();
- txtstate.setPrefColumnCount(2);
- txtstate.setEditable(false);
- txtstate.setMouseTransparent(true);
- txtstate.setFocusTraversable(false);
- txtstate.setPrefWidth(50);
- // Add to hbox
- hbox.getChildren().addAll(lblzipcode, txtzipcode, zipcityBtn, lblcity, txtcity, lblstate, txtstate);
- vbox.getChildren().addAll(hbox, separator);
- return vbox;
- }
- // Middle of program
- public VBox middlePart() {
- HBox hbox = new HBox();
- hbox.setPadding(new Insets(15, 12, 15, 12));
- hbox.setSpacing(15);
- hbox.setAlignment(Pos.CENTER);
- // VBox to hold HBox and Separator
- VBox vbox = new VBox();
- vbox.setAlignment(Pos.CENTER);
- // Add Line
- Line separator = new Line();
- separator.setStrokeWidth(5);
- separator.setStroke(Color.BLACK);
- separator.setEndX(800.0);
- separator.autosize();
- // Add Labels
- Label lblcity = new Label("City:");
- Label lblstate = new Label("State:");
- // Add Properties of TextField
- txtstate.setPrefColumnCount(2);
- txtstate.setPrefWidth(50);
- txtzipcode.setPrefColumnCount(5);
- txtzipcodes.setEditable(false);
- txtzipcodes.setMouseTransparent(true);
- txtzipcodes.setFocusTraversable(false);
- txtzipcodes.setPrefHeight(100);
- txtzipcodes.setPrefWidth(120);
- // Add to hbox
- hbox.getChildren().addAll(lblcity, txtcity, lblstate, txtstate, citytozipBtn, txtzipcodes);
- vbox.getChildren().addAll(hbox, separator);
- return vbox;
- }
- // Bottom of program
- public HBox bottomHalf() {
- HBox hbox = new HBox();
- hbox.setAlignment(Pos.CENTER);
- // Add Label
- Label lblstatus = new Label("Status:");
- // Add Properties of TextField
- txtstatus.setEditable(false);
- txtstatus.setMouseTransparent(true);
- txtstatus.setFocusTraversable(false);
- txtstatus.setPrefWidth(500);
- // Add to hbox
- hbox.getChildren().addAll(lblstatus, txtstatus);
- return hbox;
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement