gpt4 book ai didi

java - TextFlow getPrefWidth 返回 -1

转载 作者:行者123 更新时间:2023-12-01 12:39:14 25 4
gpt4 key购买 nike

我试图在添加文本对象后获取 TextFlow 的宽度和高度

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package newcodeeditor;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

/**
*
* @author Arhowk
*/
public class Tester extends Application {

@Override
public void start(Stage primaryStage) {
TextFlow t = new TextFlow();
Text t1 = new Text("test\n");


StackPane root = new StackPane();
root.getChildren().add(t);

Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();

System.out.println(t.getPrefWidth());
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

但这一直返回-1。 (最小和最大宽度也是如此)

我该如何获取流对象的宽度/高度?

最佳答案

使用

double w = textFlow.prefWidth(-1);

计算首选宽度,然后

double h = textFlow.prefHeight(w);

计算首选高度。

getPrefWidth() 方法仅返回用户可设置的 prefWidth 属性的值,默认情况下为 -1,表示“使用计算的大小”。

顺便说一句,在您发布的代码中,没有将 Text 节点添加到 TextFlow 中。

关于java - TextFlow getPrefWidth 返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25276940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com