作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在添加文本对象后获取 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/
我试图在添加文本对象后获取 TextFlow 的宽度和高度 /* * To change this license header, choose License Headers in Project
我是一名优秀的程序员,十分优秀!