【发布时间】:2021-02-11 22:27:10
【问题描述】:
我创建了一个 JavaFX 代码,它生成一个 3x3 矩阵,其中随机数从 1 到 9。 我希望代码在我更改窗口大小时更改文本大小,我希望 ti 与窗口像素一起动态变化。我听说过使用监听器,但我不知道如何使用它们。
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
int size = 500;
GridPane root = new GridPane();
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
int num = (int) (Math.random() * 9) + 1;
TextField tf = new TextField();
tf.setFont(Font.font("Ariel", FontWeight.BOLD, 50));
tf.setPrefHeight(size);
tf.setPrefWidth(size);
tf.setAlignment(Pos.CENTER);
tf.setEditable(false);
tf.setText(""+num);
root.setRowIndex(tf, y);
root.setColumnIndex(tf, x);
root.getChildren().add(tf);
}
}
Scene scene = new Scene(root, size, size);
primaryStage.setTitle("Random Binary Matrix (JavaFX)");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
【问题讨论】:
-
我听说过使用监听器,但我不知道如何使用。然后完成关于如何使用监听器的教程;)这里你想听窗口的宽度/高度属性。
标签: java javafx dynamic listener