首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TextFlow getPrefWidth返回-1

TextFlow getPrefWidth返回-1
EN

Stack Overflow用户
提问于 2014-08-13 02:02:18
回答 1查看 308关注 0票数 0

在将文本对象添加到TextFlow之后,我正在尝试获取它的宽度和高度

代码语言:javascript
复制
/*
 * 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。(最小和最大宽度也是如此)

如何获得流对象的宽度/高度?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-13 02:12:46

使用

代码语言:javascript
复制
double w = textFlow.prefWidth(-1);

若要计算首选宽度,则

代码语言:javascript
复制
double h = textFlow.prefHeight(w);

计算首选高度。

getPrefWidth()方法只返回用户可设置的prefWidth属性的值,默认情况下它是-1,意思是“使用计算大小”。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25276940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档