Write a GUI that generates a multiplication or division table. A multiplication or division table is a useful tool for learning simple multiplication and division sums.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Label[] labels = new Label[9];
VBox vBox = new VBox(50);
vBox.setAlignment(Pos.CENTER);
HBox[] rows = new HBox[3];
for (int i = 0; i < rows.length; i++) {
rows[i] = new HBox(50);
rows[i].setAlignment(Pos.CENTER);
vBox.getChildren().add(rows[i]);
}
for (int i = 0; i < labels.length; i++) {
labels[i] = new Label("");
labels[i].setFont(Font.font(16));
rows[i / 3].getChildren().add(labels[i]);
}
for (int i = 1; i < 10; i++) {
StringBuilder builder = new StringBuilder();
for (int j = 1; j < 11; j++) {
builder.append(i).append(" x ").append(j).append(" = ").append(i * j).append("\n");
}
labels[i - 1].setText(builder.toString());
}
primaryStage.setScene(new Scene(new StackPane(vBox),500,900));
primaryStage.sizeToScene();
primaryStage.setTitle("Multiplication Table");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Comments
Leave a comment