You are on page 1of 3

Code

import javafx.application.Application;

import javafx.geometry.Insets;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.CheckBox;

import javafx.scene.control.Label;

import javafx.scene.control.PasswordField;

import javafx.scene.control.TextField;

import javafx.scene.layout.GridPane;

import javafx.scene.paint.Color;

import javafx.stage.Stage;

public class CenteredLogin extends Application {

public static void main(String[] args) {

launch(args);

@Override

public void start(Stage primaryStage) {

primaryStage.setTitle("Login Page");

// Create the grid for the layout

GridPane grid = new GridPane();

grid.setAlignment(Pos.CENTER);

grid.setHgap(10);

grid.setVgap(10);
grid.setPadding(new Insets(20, 20, 20, 20));

// Create controls

Label loginFormLabel = new Label("Login Form");

Label usernameLabel = new Label("Username:");

TextField usernameField = new TextField();

Label passwordLabel = new Label("Password:");

PasswordField passwordField = new PasswordField();

CheckBox rememberCheckBox = new CheckBox("Remember Me");

rememberCheckBox.setTextFill(Color.WHITE); // Optional: Set text color for the checkbox

Button loginButton = new Button("Login");

loginButton.setStyle("-fx-background-color: #2196F3; -fx-text-fill: white;");

loginButton.setOnAction(e -> handleLogin(usernameField.getText(), passwordField.getText()));

// Add controls to the grid

grid.add(loginFormLabel, 0, 0, 2, 1);

grid.add(usernameLabel, 0, 1);

grid.add(usernameField, 1, 1);

grid.add(passwordLabel, 0, 2);

grid.add(passwordField, 1, 2);

grid.add(rememberCheckBox, 1, 3);

grid.add(loginButton, 1, 4);

// Create the scene and set it to the stage

Scene scene = new Scene(grid, 300, 200);

primaryStage.setScene(scene);

// Show the stage

primaryStage.show();
}

private void handleLogin(String username, String password) {

// Replace this with your actual authentication logic

if ("user123".equals(username) && "pass123".equals(password)) {

System.out.println("Login successful!");

} else {

System.out.println("Login failed. Please check your credentials.");

You might also like