0% found this document useful (0 votes)
54 views6 pages

23mca1097 Ex9 Java

The document describes two JavaFX programs. The first program creates a login screen with labels, text fields, and a button. The second program creates a simple calculator app with labels, text fields, and buttons to perform basic math operations.

Uploaded by

Piyush Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views6 pages

23mca1097 Ex9 Java

The document describes two JavaFX programs. The first program creates a login screen with labels, text fields, and a button. The second program creates a simple calculator app with labels, text fields, and buttons to perform basic math operations.

Uploaded by

Piyush Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EX-9: Java FX

NAME: Parekh Divy REG: 23MCA1097

1.

CODE:
package application;
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.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
// Creating labels, text fields, and button
Label usernameLabel = new Label("Username:");
TextField usernameField = new TextField();

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


PasswordField passwordField = new PasswordField();

Button loginButton = new Button("Login");

// Setting up VBox layout


VBox root = new VBox(10); // Spacing between elements
root.setPadding(new Insets(20)); // Padding around the layout
root.setAlignment(Pos.CENTER); // Center alignment

// Adding elements to the VBox


root.getChildren().addAll(usernameLabel, usernameField,
passwordLabel, passwordField, loginButton);

// Handling login button click


loginButton.setOnAction(e -> {
String username = usernameField.getText();
String password = passwordField.getText();
// Dummy login logic, replace it with your actual
authentication logic
boolean loginSuccessful = dummyLogin(username, password);

if (loginSuccessful) {
System.out.println("Login successful!");
} else {
System.out.println("Login failed. Please try again.");
}
});

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


primaryStage.setScene(scene);
primaryStage.setTitle("Login Screen");
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}

// Dummy login method, replace it with your actual authentication logic


private boolean dummyLogin(String username, String password) {
// Dummy credentials
String validUsername = "admin";
String validPassword = "password";

return username.equals(validUsername) &&


password.equals(validPassword);
}
public static void main(String[] args) {
launch(args);
}
}
2.
CODE:

package application;
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.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
// Create labels
Label num1Label = new Label("Number 1:");
Label num2Label = new Label("Number 2:");
Label resultLabel = new Label("Result:");
// Create text fields
TextField num1Field = new TextField();
TextField num2Field = new TextField();
TextField resultField = new TextField();
resultField.setEditable(false); // Result field is not editable by
the user
// Create buttons
Button addButton = new Button("+");
Button subtractButton = new Button("-");
Button multiplyButton = new Button("*");
Button divideButton = new Button("/");
// Set event handlers for the buttons
addButton.setOnAction(e -> calculateResult(num1Field, num2Field,
resultField, "+"));
subtractButton.setOnAction(e -> calculateResult(num1Field,
num2Field, resultField, "-"));
multiplyButton.setOnAction(e -> calculateResult(num1Field,
num2Field, resultField, "*"));
divideButton.setOnAction(e -> calculateResult(num1Field,
num2Field, resultField, "/"));
// Create layout
GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);
root.setHgap(10);
root.setVgap(10);
root.setPadding(new Insets(20));
// Add components to the layout
root.add(num1Label, 0, 0);
root.add(num1Field, 1, 0);
root.add(num2Label, 0, 1);
root.add(num2Field, 1, 1);
root.add(addButton, 0, 2);
root.add(subtractButton, 1, 2);
root.add(multiplyButton, 0, 3);
root.add(divideButton, 1, 3);
root.add(resultLabel, 0, 4);
root.add(resultField, 1, 4);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.setTitle("Simple Calculator");
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
private void calculateResult(TextField num1Field, TextField num2Field,
TextField resultField, String operator) {
try {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
double result = 0.0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 == 0) {
resultField.setText("Error: Division by zero");
return;
}
result = num1 / num2;
break;
default:
break;
}
resultField.setText(String.valueOf(result));
} catch (NumberFormatException ex) {
resultField.setText("Error: Invalid input");
}
}
public static void main(String[] args) {
launch(args);
}
}

You might also like