【问题标题】:How can I make a dynamic font in a textfield in javaFX如何在 javaFX 的文本字段中制作动态字体
【发布时间】: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


【解决方案1】:

您可以在 Gridpane 上为 widthProperty()heightProperty() 添加侦听器。对于你的情况,你可以尝试这样的事情:

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);

                ChangeListener listener=(observable, oldValue, newValue) -> {
                    changeFontSize(root,tf);
                };
                root.heightProperty().addListener(listener);
                root.widthProperty().addListener(listener);
            }
        }
        Scene scene = new Scene(root, size, size);
        primaryStage.setTitle("Random Binary Matrix (JavaFX)");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void changeFontSize(GridPane root, TextField tf) {
        double min = Math.min(root.getWidth(), root.getHeight());
        tf.setFont(Font.font("Ariel", FontWeight.BOLD, min/10));
    }

    public static void main(String[] args) {
        launch(args);
    }
}

【讨论】:

  • 为什么要为同一个属性注册多个监听器?
  • 是的,我知道您只需一个听众就可以做到:
  • 重复:你注册了多少个监听器到 root.width/heightProperty ;)
  • 一个监听器错了 - 正是我假设你的想法;)提示:你在一个循环中做,实际上是一个嵌套循环......
  • "One listener for both wrong" 除非您在优化方面有更好的建议,否则我不知道您所说的“错误”是什么意思,because my solution clearly works
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多