Advertisement
CE_162

FINAL EXAM *PHASE 1* The GUI

May 10th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1.  
  2. /*
  3.  * Collin Emond
  4.  * 5-10-16
  5.  * Final Exam
  6.  * CSC-112
  7.  * Prof. Silvestri
  8.  * Phase 1: The GUI
  9.  * Create a GUI that implements error checking for the ZipCode Translator
  10.  * cremond0001@student.stcc.edu
  11.  */
  12.  
  13. import javafx.application.Application;
  14. import javafx.geometry.Insets;
  15. import javafx.geometry.Pos;
  16. import javafx.scene.Scene;
  17. import javafx.scene.control.*;
  18. import javafx.scene.layout.*;
  19. import javafx.scene.paint.Color;
  20. import javafx.scene.shape.Line;
  21. import javafx.stage.Stage;
  22.  
  23. public class ZipCodeTranslatorGUI extends Application {
  24.     TextField txtzipcode = new TextField();
  25.     TextField txtcity = new TextField();
  26.     TextField txtstate = new TextField();
  27.     Button zipcityBtn = new Button("Zip To City");
  28.     Button citytozipBtn = new Button("City to Zip");
  29.     TextField txtzipcodes = new TextField("Zip Code(s)");
  30.  
  31.     @Override
  32.     public void start(Stage primaryStage) {
  33.  
  34.         // Create a new pane to hold
  35.         BorderPane pane = new BorderPane();
  36.         pane.setTop(topHalf());
  37.         pane.setCenter(middlePart());
  38.         pane.setBottom(bottomHalf());
  39.         zipcityBtn.setOnAction(e -> checkzipcity());
  40.         citytozipBtn.setOnAction(e -> checkcitytozip());
  41.  
  42.         // Create a scene
  43.         Scene scene = new Scene(pane, 800, 300);
  44.         primaryStage.setTitle("Zip Code to City/State Translator App");
  45.         primaryStage.setScene(scene);
  46.         primaryStage.show();
  47.         primaryStage.setResizable(false);
  48.     }
  49.  
  50.     // TextStatus Field
  51.     TextField txtstatus = new TextField();
  52.  
  53.     // Method that executes Zip to City Button Code
  54.     private void checkzipcity() {
  55.         if (txtzipcode.getText().matches("(\\d{5})")) {
  56.             txtstatus.setText("Valid Zip Code");
  57.         } else {
  58.             txtstatus.setText("Not a Valid Zip Code");
  59.         }
  60.     }
  61.  
  62.     // Method that executes City to Zip Button Code
  63.     private void checkcitytozip() {
  64.         if (txtcity.getText().matches("^[a-zA-Z\\s-]+$") && txtstate.getText().matches("^[A-Za-z]{2}")) {
  65.             txtstatus.setText("Valid City and State");
  66.         } else {
  67.             txtstatus.setText("Not a Valid City and State");
  68.         }
  69.     }
  70.  
  71.     // Top Half of Program
  72.     public VBox topHalf() {
  73.  
  74.         HBox hbox = new HBox();
  75.         hbox.setPadding(new Insets(15, 12, 15, 12));
  76.         hbox.setSpacing(15);
  77.         hbox.setAlignment(Pos.CENTER);
  78.  
  79.         // VBox to hold HBox and Separator
  80.         VBox vbox = new VBox();
  81.         vbox.setAlignment(Pos.CENTER);
  82.  
  83.         // Add Line
  84.         Line separator = new Line();
  85.         separator.setStrokeWidth(5);
  86.         separator.setStroke(Color.BLACK);
  87.         separator.setEndX(800.0);
  88.         separator.autosize();
  89.  
  90.         // Add Labels
  91.         Label lblzipcode = new Label("Zip Code:");
  92.         Label lblcity = new Label("City:");
  93.         Label lblstate = new Label("State:");
  94.  
  95.         // Add Properties of TextField
  96.         txtzipcode.setPrefColumnCount(5);
  97.         txtzipcode.setPrefWidth(80);
  98.         TextField txtcity = new TextField();
  99.         txtcity.setPrefColumnCount(25);
  100.         txtcity.setPrefWidth(80);
  101.         txtcity.setEditable(false);
  102.         txtcity.setMouseTransparent(true);
  103.         txtcity.setFocusTraversable(false);
  104.         TextField txtstate = new TextField();
  105.         txtstate.setPrefColumnCount(2);
  106.         txtstate.setEditable(false);
  107.         txtstate.setMouseTransparent(true);
  108.         txtstate.setFocusTraversable(false);
  109.         txtstate.setPrefWidth(50);
  110.  
  111.         // Add to hbox
  112.         hbox.getChildren().addAll(lblzipcode, txtzipcode, zipcityBtn, lblcity, txtcity, lblstate, txtstate);
  113.         vbox.getChildren().addAll(hbox, separator);
  114.         return vbox;
  115.     }
  116.  
  117.     // Middle of program
  118.     public VBox middlePart() {
  119.  
  120.         HBox hbox = new HBox();
  121.         hbox.setPadding(new Insets(15, 12, 15, 12));
  122.         hbox.setSpacing(15);
  123.         hbox.setAlignment(Pos.CENTER);
  124.  
  125.         // VBox to hold HBox and Separator
  126.         VBox vbox = new VBox();
  127.         vbox.setAlignment(Pos.CENTER);
  128.  
  129.         // Add Line
  130.         Line separator = new Line();
  131.         separator.setStrokeWidth(5);
  132.         separator.setStroke(Color.BLACK);
  133.         separator.setEndX(800.0);
  134.         separator.autosize();
  135.  
  136.         // Add Labels
  137.         Label lblcity = new Label("City:");
  138.         Label lblstate = new Label("State:");
  139.  
  140.         // Add Properties of TextField
  141.         txtstate.setPrefColumnCount(2);
  142.         txtstate.setPrefWidth(50);
  143.         txtzipcode.setPrefColumnCount(5);
  144.         txtzipcodes.setEditable(false);
  145.         txtzipcodes.setMouseTransparent(true);
  146.         txtzipcodes.setFocusTraversable(false);
  147.         txtzipcodes.setPrefHeight(100);
  148.         txtzipcodes.setPrefWidth(120);
  149.  
  150.         // Add to hbox
  151.         hbox.getChildren().addAll(lblcity, txtcity, lblstate, txtstate, citytozipBtn, txtzipcodes);
  152.         vbox.getChildren().addAll(hbox, separator);
  153.  
  154.         return vbox;
  155.     }
  156.  
  157.     // Bottom of program
  158.     public HBox bottomHalf() {
  159.  
  160.         HBox hbox = new HBox();
  161.         hbox.setAlignment(Pos.CENTER);
  162.  
  163.         // Add Label
  164.         Label lblstatus = new Label("Status:");
  165.  
  166.         // Add Properties of TextField
  167.         txtstatus.setEditable(false);
  168.         txtstatus.setMouseTransparent(true);
  169.         txtstatus.setFocusTraversable(false);
  170.         txtstatus.setPrefWidth(500);
  171.  
  172.         // Add to hbox
  173.         hbox.getChildren().addAll(lblstatus, txtstatus);
  174.  
  175.         return hbox;
  176.     }
  177.  
  178.     public static void main(String[] args) {
  179.         launch(args);
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement