Abdullah 2
Abdullah 2
greeting message when a user enters their name and clicks a button.
When the user enters their name (e.g., "Alice") in the text field and clicks the "Say Hello" button,
the label updates to display "Hello, Alice!".
Requirements
Code
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@Override
sayHelloButton.setOnAction(e -> {
if (!name.isEmpty()) {
} else {
});
primaryStage.setScene(scene);
primaryStage.setTitle("Greeting Application");
primaryStage.show();
launch(args);
Task 2: You are tasked with creating a JavaFX application that changes the background color of
the window when the user clicks a button.
Requirements
Code
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.Random;
@Override
changeColorButton.setLayoutX(100);
changeColorButton.setLayoutY(100);
pane.getChildren().add(changeColorButton);
changeColorButton.setOnAction(e -> {
});
// Create the Scene with the pane
primaryStage.setScene(scene);
primaryStage.show();
return String.format("#%02X%02X%02X",
(int)(color.getRed() * 255),
(int)(color.getGreen() * 255),
(int)(color.getBlue() * 255));
You are tasked with creating a JavaFX application that increments a counter every time a button
is clicked.
Requirements
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@Override
// Set the Button action to increment the counter and update the Label
incrementButton.setOnAction(e -> {
counter++;
counterLabel.setText(String.valueOf(counter));
});
primaryStage.setScene(scene);
primaryStage.setTitle("Counter Application");
primaryStage.show();
}
: Create a simple JavaFX application to convert temperatures between Celsius and Fahrenheit.
Task Description:
1. Layout:
Add a Label for the title (e.g., "Temperature Converter").
Add two TextField components for entering the temperature in Celsius and
Fahrenheit.
Include two Buttons labeled "Convert to Fahrenheit" and "Convert to Celsius".
Arrange the components using a VBox.
2. Functionality:
Clicking "Convert to Fahrenheit" should calculate the Fahrenheit equivalent and
display it in the appropriate TextField.
Clicking "Convert to Celsius" should calculate the Celsius equivalent and display
it.
3. Validation:
Display an error message in a Label if the user enters invalid input.
4. Enhancement:
Add tooltips to guide users on how to use the application.
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.Tooltip;
@Override
public void start(Stage primaryStage) {
celsiusField.setPromptText("Enter Celsius");
Tooltip.install(celsiusField, celsiusTooltip);
fahrenheitField.setPromptText("Enter Fahrenheit");
Tooltip.install(fahrenheitField, fahrenheitTooltip);
toFahrenheitButton.setOnAction(e -> {
try {
double celsius = Double.parseDouble(celsiusField.getText());
fahrenheitField.setText(String.format("%.2f", fahrenheit));
});
toCelsiusButton.setOnAction(e -> {
try {
celsiusField.setText(String.format("%.2f", celsius));
});
primaryStage.setScene(scene);
primaryStage.setTitle("Temperature Converter");
primaryStage.show();
launch(args);
}
Home Task 2: To-Do List Manager
Build a basic To-Do List application.
Task Description:
1. Layout:
Add a TextField for entering a task.
Include an "Add Task" button to add the task to a list.
Use a ListView to display the tasks.
Add a "Remove Selected Task" button to delete a selected task.
Arrange components using a VBox.
2. Functionality:
Clicking "Add Task" should add the entered task to the ListView.
Clicking "Remove Selected Task" should delete the selected task from the ListView.
3. Enhancement:
Add a confirmation dialog (Alert) before deleting a task.
Code
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@Override
taskField.setPromptText("Enter a task");
Tooltip.install(taskField, taskTooltip);
addTaskButton.setOnAction(e -> {
if (!task.isEmpty()) {
taskListView.getItems().add(task);
taskField.clear();
} else {
});
removeTaskButton.setOnAction(e -> {
if (selectedTask != null) {
confirmationAlert.showAndWait().ifPresent(response -> {
if (response == AlertType.CONFIRMATION.getButtonData()) {
taskListView.getItems().remove(selectedTask);
}
});
} else {
});
primaryStage.setScene(scene);
primaryStage.show();
launch(args);
}
}
: Simple Calculator
Scenario: Create a basic calculator application to perform addition, subtraction, multiplication,
and division.
Task Description:
1. Layout:
Add two TextField components for number input.
Include four Buttons for "+", "-", "*", and "/".
Add a Label to display the result.
Use an HBox for the buttons and a VBox for the overall layout.
2. Functionality:
Clicking a button should perform the corresponding operation and display the result in
the Label.
Handle division by zero with an error message in the Label.
3. Validation:
Ensure both inputs are valid numbers before performing any operation.
Code
import javafx.application.Application;
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.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@Override
buttonBox.setAlignment(Pos.CENTER);
layout.setAlignment(Pos.CENTER);
addButton.setOnAction(e -> {
try {
double num1 = Double.parseDouble(numberField1.getText());
});
subtractButton.setOnAction(e -> {
try {
});
multiplyButton.setOnAction(e -> {
try {
});
divideButton.setOnAction(e -> {
try {
if (num2 == 0) {
} else {
});
primaryStage.setTitle("Simple Calculator");
primaryStage.show();
launch(args);