Advertisement
luliu

A Zip code to City/State Translator Database GUI

May 10th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. /*
  2.  * Name: Lu Liu
  3.  * Date: 5/10/2016
  4.  * Course Number: CSC-112
  5.  * Course Name: Intermediate Topics in Java Programming
  6.  * Email: lliu0001@student.stcc.edu
  7.  *
  8.  * Assignment: HW # 17
  9.  * Programe Description:
  10.  * A Zip code to City/State Translator Database GUI
  11.  */
  12. import java.util.Scanner;
  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.Button;
  18. import javafx.scene.control.Label;
  19. import javafx.scene.control.TextArea;
  20. import javafx.scene.control.TextField;
  21. import javafx.scene.layout.BorderPane;
  22. import javafx.scene.layout.HBox;
  23. import javafx.scene.paint.Color;
  24. import javafx.scene.shape.Line;
  25. import javafx.stage.Stage;
  26.  
  27. public class ZipCodeTranslationGUI extends Application {
  28.     public void start(Stage primaryStage) {
  29.         // Create pane to hold the label and text field
  30.         HBox hBox = new HBox(10);
  31.         TextField tfZipCode = new TextField();
  32.         tfZipCode.setAlignment(Pos.CENTER_LEFT);
  33.         TextField tfCITY = new TextField();
  34.         tfCITY.setAlignment(Pos.CENTER_LEFT);
  35.         TextField tfSTATE = new TextField();
  36.         tfSTATE.setAlignment(Pos.CENTER_LEFT);
  37.         Button bnToCity = new Button("Zip to City");
  38.         hBox.setPadding(new Insets(5, 15, 25, 5));
  39.         hBox.getChildren().addAll(new Label("Zip Code:"), tfZipCode, bnToCity, new Label("City:"), tfCITY,
  40.                 new Label("State:"), tfSTATE);
  41.  
  42.         HBox hBox2 = new HBox(10);
  43.         hBox2.setPadding(new Insets(5, 15, 25, 5));
  44.         TextField tfCity = new TextField();
  45.         tfCity.setAlignment(Pos.CENTER_LEFT);
  46.         TextField tfState = new TextField();
  47.         tfState.setAlignment(Pos.CENTER_LEFT);
  48.         Button bnToZip = new Button("City to Zip");
  49.         TextArea ta = new TextArea();
  50.         ta.setMaxSize(130, 130);
  51.         ta.setText("Zip Codes");
  52.         hBox2.getChildren().addAll(new Label("City:"), tfCity, new Label("State:"), tfState, bnToZip, ta);
  53.         Line line1 = new Line(10, 10, 750, 10);
  54.         line1.setStrokeWidth(3);
  55.         line1.setStroke(Color.WHITE);
  56.         BorderPane centerPane = new BorderPane();
  57.         centerPane.setTop(line1);
  58.         centerPane.setCenter(hBox2);
  59.  
  60.         HBox hBox3 = new HBox(10);
  61.         hBox3.setPadding(new Insets(5, 15, 25, 80));
  62.         TextField tfStatus = new TextField();
  63.         tfStatus.setMinSize(510, 20);
  64.         tfStatus.setAlignment(Pos.CENTER);
  65.         hBox3.getChildren().addAll(new Label("Status:"), tfStatus);
  66.         Line line2 = new Line(10, 200, 750, 200);
  67.         line2.setStrokeWidth(3);
  68.         line2.setStroke(Color.WHITE);
  69.         BorderPane bottomPane = new BorderPane();
  70.         bottomPane.setCenter(hBox3);
  71.         bottomPane.setTop(line2);
  72.  
  73.         // Create pane to hold the panefor all
  74.         BorderPane mainPane = new BorderPane();
  75.         mainPane.setPadding(new Insets(25, 5, 5, 5));
  76.         mainPane.setStyle("-fx-background-color: #D3D3D3;");
  77.         mainPane.setTop(hBox);
  78.         mainPane.setCenter(centerPane);
  79.         mainPane.setBottom(bottomPane);
  80.  
  81.         // catch the invalid input
  82.         bnToCity.setOnAction(e -> {
  83.             try {
  84.                 int zipCode = Integer.parseInt(tfZipCode.getText());
  85.                 if (tfZipCode.getText().length() == 5) {
  86.                     tfStatus.setText("Valid Zip Code");
  87.                 } else {
  88.                     tfStatus.setText("The ZIP Code must be 5 digits integer");
  89.                 }
  90.             } catch (Exception ex) {
  91.                 tfStatus.setText("The ZIP Code must be 5 digits integer");
  92.             }
  93.             // bnToCity.fire();
  94.         });
  95.  
  96.         // Create a scene and place it in the stage
  97.         Scene scene = new Scene(mainPane, 760, 300);
  98.         primaryStage.setTitle("Zip Code Translation System"); // Set the stage
  99.         primaryStage.setScene(scene); // Place the scene in the stage
  100.         primaryStage.show(); // Display the stage
  101.  
  102.     }
  103.  
  104.     public static void main(String[] args) {
  105.         launch(args);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement