Advertisement
Guest User

HvA Workshop JavaFX

a guest
Oct 14th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. package nl.hva.fonlc;
  2.  
  3. import javafx.application.Application;
  4. import javafx.event.ActionEvent;
  5. import javafx.event.EventHandler;
  6. import javafx.geometry.Pos;
  7. import javafx.scene.Scene;
  8. import javafx.scene.control.Button;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.control.PasswordField;
  11. import javafx.scene.control.TextField;
  12. import javafx.scene.layout.BorderPane;
  13. import javafx.scene.layout.HBox;
  14. import javafx.scene.layout.VBox;
  15. import javafx.stage.Stage;
  16.  
  17. /**
  18.  *
  19.  * @author lennardf1989
  20.  */
  21. public class MainApplication extends Application {
  22.    
  23.     @Override
  24.     public void start(Stage primaryStage) {
  25.         BorderPane root = new BorderPane();
  26.        
  27.         //Center        
  28.         VBox centerBox = new VBox();
  29.         centerBox.setStyle("-fx-max-width:200px;-fx-alignment:center");
  30.  
  31.         Label statusLabel = new Label();
  32.         statusLabel.setVisible(false);
  33.         centerBox.getChildren().add(statusLabel);
  34.        
  35.         Label userNameLabel = new Label("Username");
  36.         centerBox.getChildren().add(userNameLabel);
  37.        
  38.         TextField userNameTextField = new TextField();
  39.         centerBox.getChildren().add(userNameTextField);
  40.        
  41.         Label passwordLabel = new Label("Password");
  42.         centerBox.getChildren().add(passwordLabel);
  43.        
  44.         PasswordField passwordTextField = new PasswordField();
  45.         centerBox.getChildren().add(passwordTextField);
  46.        
  47.         root.setCenter(centerBox);
  48.        
  49.         //Bottom
  50.         HBox bottomBox = new HBox();
  51.         bottomBox.setAlignment(Pos.BASELINE_RIGHT);
  52.         bottomBox.setStyle("-fx-padding:20px");
  53.        
  54.         Button loginButton = new Button("Login");
  55.         loginButton.setOnAction(new EventHandler<ActionEvent>() {
  56.             @Override
  57.             public void handle(ActionEvent event) {
  58.                 statusLabel.setVisible(true);
  59.                        
  60.                 boolean userName = false;
  61.                 boolean password = false;
  62.                
  63.                 if(userNameTextField.getText().equals("")) {
  64.                     userNameLabel.setStyle("-fx-text-fill:red");
  65.                 }
  66.                 else {
  67.                     userNameLabel.setStyle("-fx-text-fill:inherit");
  68.                    
  69.                     userName = true;
  70.                 }
  71.                
  72.                 if(passwordTextField.getText().equals("")) {
  73.                     passwordLabel.setStyle("-fx-text-fill:red");
  74.                 }
  75.                 else {
  76.                     passwordLabel.setStyle("-fx-text-fill:inherit");
  77.                    
  78.                     password = true;
  79.                 }
  80.                
  81.                 if(userName && password) {
  82.                     statusLabel.setText("You are logged in!");
  83.                 }
  84.                 else {
  85.                     statusLabel.setText("Username and password are both required!");
  86.                 }
  87.             }
  88.         });
  89.         bottomBox.getChildren().add(loginButton);
  90.        
  91.         root.setBottom(bottomBox);
  92.        
  93.         //Scene
  94.         Scene scene = new Scene(root, 640, 480);
  95.        
  96.         //Window
  97.         primaryStage.setTitle("Hello World!");
  98.         primaryStage.setScene(scene);
  99.         primaryStage.show();
  100.     }
  101.  
  102.     /**
  103.      * @param args the command line arguments
  104.      */
  105.     public static void main(String[] args) {
  106.         launch(args);
  107.     }
  108.    
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement