在将文本对象添加到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。(最小和最大宽度也是如此)
如何获得流对象的宽度/高度?
发布于 2014-08-13 02:12:46
使用
double w = textFlow.prefWidth(-1);若要计算首选宽度,则
double h = textFlow.prefHeight(w);计算首选高度。
getPrefWidth()方法只返回用户可设置的prefWidth属性的值,默认情况下它是-1,意思是“使用计算大小”。
顺便说一下,在您发布的代码中,没有将Text节点添加到TextFlow中。
https://stackoverflow.com/questions/25276940
复制相似问题