java 获取节点的维度 - JavaFX 2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12670809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:47:07  来源:igfitidea点击:

Get the Dimension of a Node - JavaFX 2

javajavafx-2

提问by Giannis Krimitzas

I am trying to jump to JavaFX from Swing. But I can't find how to get the width and the height of a node.

我正在尝试从 Swing 跳转到 JavaFX。但是我找不到如何获取节点的宽度和高度。

So, here is a bit of code.

所以,这里有一些代码。

Label label = new Label();
label.setText("Hello");
label.setFont(new Font(32));

System.out.println(label.getPrefWidth());
System.out.println(label.getWidth());
System.out.println(label.getMinWidth());
System.out.println(label.getMaxWidth());

The results are:

结果是:

-1.0
 0.0
-1.0
-1.0

The same thing in Swing is:

在 Swing 中,同样的事情是:

JComponent.getPreferredSize().width
JComponent.getPreferredSize().height

Thank you

谢谢



After edit:

编辑后:

Why this isn't working for me?

为什么这对我不起作用?

public class Dimensions extends Application {

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

    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");

        primaryStage.setScene(new Scene(new MyPanel(), 500, 500));
        primaryStage.centerOnScreen();
        primaryStage.setResizable(false);
        primaryStage.show();
    }
}
public class MyPanel extends Pane {

    public MyPanel() {  
        Label label = new Label();
        label.setText("Hello");
        label.setFont(new Font(32));

        getChildren().add(label);

        label.relocate(150, 150);

        System.out.println(label.getWidth());
    }
}

回答by Tsangares

Once the node is added to the screen you can use:

将节点添加到屏幕后,您可以使用:

thisNode.getBoundsInParent().getHeight(); //Returns height of object in parent container.

回答by Sergey Grinev

Width and height are not initialized until you put node in a container actually placed on the scene, because they can change depending on container type.

宽度和高度不会被初始化,直到您将节点放入实际放置在场景中的容器中,因为它们可以根据容器类型而改变。

Try next:

接下来试试:

public void start(Stage primaryStage) {
    Label label = new Label();
    label.setText("Hello");
    label.setFont(Font.font("Arial", 32));

    System.out.println(label.getWidth());

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

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

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

    System.out.println("------");
    System.out.println(label.getWidth());
}

In your code make next changes:

在您的代码中进行下一步更改:

public class Dimensions extends Application {

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

    public void start(Stage primaryStage) {
        MyPanel myPanel = new MyPanel();
        primaryStage.setScene(new Scene(myPanel, 500, 500));
        primaryStage.show();

        System.out.println(myPanel.label.getWidth());
    }
}

class MyPanel extends Pane {
    public Label label;

    public MyPanel() {

        label = new Label();
        label.setText("Hello");
        label.setFont(new Font(32));
        getChildren().add(label);
        label.relocate(150, 150);
    }
}