Java Homework
Java Homework
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;
areaRadioButton.setToggleGroup(toggleGroup);
boundaryRadioButton.setToggleGroup(toggleGroup);
areaRadioButton.setSelected(true);
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.setAlignment(Pos.CENTER_RIGHT);
hbox.getChildren().add(computeButton);
gridPane.add(hbox, 0, 5);
hbox1.getChildren().add(closeButton);
gridPane.add(hbox1, 2, 5);
stage.setScene(scene);
stage.setTitle("Rectangle Calculator");
stage.show();
}
private void compute() {
if (areaRadioButton.isSelected()) {
resultField.setText(String.format("%.2f", area));
} else {
resultField.setText(String.format("%.2f", boundary));
return closeButton;
launch(args);
}
OUTPUT: