You are on page 1of 6

Student Name: Noorhan Sadam Alazzam……32119001015

Write complete JavaFX application that takes width and height for a given rectangle then
computes boundary or area according to user selection
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.Label;

import javafx.scene.control.RadioButton;

import javafx.scene.control.TextField;

import javafx.scene.control.ToggleGroup;

import javafx.scene.layout.GridPane;

import javafx.scene.layout.HBox;

import javafx.stage.Stage;

public class RectangleCalculator extends Application {

private TextField widthField;

private TextField heightField;

private TextField resultLabel1;

private RadioButton areaRadioButton;

private RadioButton boundaryRadioButton;

private TextField resultField;


@Override

public void start(Stage stage) throws Exception {

// Create the UI elements

Label widthLabel = new Label("Enter Width:");

widthField = new TextField();

Label heightLabel = new Label("Enter Height:");

heightField = new TextField();

areaRadioButton = new RadioButton("Area");

boundaryRadioButton = new RadioButton("Boundary");

Label resultLabel1 = new Label("result:");

resultField = new TextField();

Button computeButton = new Button("compute");

Button closeButton = new Button("close");

// Set up the radio buttons

ToggleGroup toggleGroup = new ToggleGroup();

areaRadioButton.setToggleGroup(toggleGroup);

boundaryRadioButton.setToggleGroup(toggleGroup);

areaRadioButton.setSelected(true);

// Set up the UI layout

GridPane gridPane = new GridPane();

gridPane.setHgap(10);

gridPane.setVgap(10);

gridPane.setPadding(new Insets(10));

gridPane.add(widthLabel, 0, 0);
gridPane.add(widthField, 1, 0);

gridPane.add(heightLabel, 0, 1);

gridPane.add(heightField, 1, 1);

gridPane.add(resultLabel1, 0, 3);

gridPane.add(resultField, 1,3);

gridPane.add(areaRadioButton, 0, 4);

gridPane.add(boundaryRadioButton, 1, 4);

HBox hbox = new HBox(10);

HBox hbox1 = new HBox(10);

hbox.setAlignment(Pos.CENTER_RIGHT);

hbox.getChildren().add(computeButton);

gridPane.add(hbox, 0, 5);

hbox1.getChildren().add(closeButton);

gridPane.add(hbox1, 2, 5);

// Set up the event handlers

computeButton.setOnAction(event -> compute());

closeButton.setOnAction(event -> stage.close());

// Set up the scene and show the stage

Scene scene = new Scene(gridPane);

stage.setScene(scene);

stage.setTitle("Rectangle Calculator");

stage.show();

}
private void compute() {

// Get the input values from the text fields

double width = Double.parseDouble(widthField.getText());

double height = Double.parseDouble(heightField.getText());

// Compute the result based on the selected radio button

if (areaRadioButton.isSelected()) {

double area = width * height;

resultField.setText(String.format("%.2f", area));

} else {

double boundary = 2 * (width + height);

resultField.setText(String.format("%.2f", boundary));

private Button close(Stage stage){

Button closeButton = new Button("Close Window");

closeButton.setOnAction(event -> stage.close());

return closeButton;

public static void main(String[] args) {

launch(args);

}
OUTPUT:

You might also like