MIT-Curricular/OOP/Java/Lab/Week13/Multiplier.java

67 lines
2.1 KiB
Java

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Multiplier extends Application {
@Override
public void start(Stage primaryStage) {
FlowPane root = new FlowPane();
root.setPadding(new Insets(10));
root.setHgap(10);
root.setVgap(10);
Label inputLabel = new Label("Enter a number:");
TextField numberInput = new TextField();
Button generateButton = new Button("Generate Table");
TextArea resultArea = new TextArea();
resultArea.setPrefRowCount(12);
resultArea.setPrefColumnCount(20);
resultArea.setEditable(false);
generateButton.setOnAction(e -> {
try {
int number = Integer.parseInt(numberInput.getText());
StringBuilder table = new StringBuilder();
table
.append("Multiplication Table for ")
.append(number)
.append("\n\n");
for (int i = 1; i <= 10; i++) {
table
.append(number)
.append(" x ")
.append(i)
.append(" = ")
.append(number * i)
.append("\n");
}
resultArea.setText(table.toString());
} catch (NumberFormatException ex) {
resultArea.setText("Please enter a valid integer");
}
});
root
.getChildren()
.addAll(inputLabel, numberInput, generateButton, resultArea);
Scene scene = new Scene(root, 300, 400);
primaryStage.setTitle("Multiplication Table Generator");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}