Skip to content

Instantly share code, notes, and snippets.

@btownrippleman
Created February 26, 2020 15:19
Show Gist options
  • Select an option

  • Save btownrippleman/3e7c16a1ecc2de6ccc480190eef98720 to your computer and use it in GitHub Desktop.

Select an option

Save btownrippleman/3e7c16a1ecc2de6ccc480190eef98720 to your computer and use it in GitHub Desktop.
javafx code for basic login
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.application.Application;
import javafx.beans.binding.NumberBinding;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.Group;
import javafx.stage.Stage;
public class Main extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
@Override
public void start(Stage stage)
{
// Create the Label for the Name
Label nameLbl = new Label("Name:");
// Set the location for the Label
nameLbl.setLayoutX(10);
nameLbl.setLayoutY(10);
// Create the TextField for the Name
TextField nameFld = new TextField();
// Set the location of the Name TextField relative to the Name Label
NumberBinding nameFldXBinding =
nameLbl.layoutXProperty().add(nameLbl.widthProperty().add(10));
nameFld.layoutXProperty().bind(nameFldXBinding);
nameFld.layoutYProperty().bind(nameLbl.layoutYProperty());
// Create the Label for the Password
Label passwordLbl = new Label("Password:");
// Set the location of the Password Label relative to the Name TextField
NumberBinding passwordLblXBinding =
nameFld.layoutXProperty().add(nameFld.widthProperty().add(10));
passwordLbl.layoutXProperty().bind(passwordLblXBinding);
passwordLbl.layoutYProperty().bind(nameFld.layoutYProperty());
// Create the TextField for the Password
TextField passwordFld = new TextField();
// Set the location of the Password TextField relative to the Password Label
NumberBinding passwordFldXBinding =
passwordLbl.layoutXProperty().add(passwordLbl.widthProperty().add(10));
passwordFld.layoutXProperty().bind(passwordFldXBinding);
passwordFld.layoutYProperty().bind(passwordLbl.layoutYProperty());
// Create the Login-Button
Button loginBtn = new Button("Login");
EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
public void handle(ActionEvent e)
{
System.out.println("username: "+ nameFld.getText()+"\npassword: "+passwordFld.getText());
}
};
// when button is pressed
loginBtn.setOnAction(event);
// Set the location of the Login Button relative to the Password TextField
NumberBinding loginBtnXBinding =
passwordFld.layoutXProperty().add(passwordFld.widthProperty().add(10));
loginBtn.layoutXProperty().bind(loginBtnXBinding);
loginBtn.layoutYProperty().bind(passwordFld.layoutYProperty());
// Create the Group
Group root = new Group();
// Add the children to the Group
root.getChildren().addAll(nameLbl, nameFld, passwordLbl, passwordFld, loginBtn);
// Create the Scene
Scene scene = new Scene(root,600,100);
// Add the scene to the Stage
stage.setScene(scene);
// Set the title of the Stage
stage.setTitle("A Group Example");
// Display the Stage
stage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment