简体   繁体   English

绑定BorderPane和框架的高度和宽度

[英]Binding BorderPane and Frame Height and Width

I am using JavaFX 2 in Netbeans. 我在Netbeans中使用JavaFX 2。 How do I get the width and height of an element to adjust when the Frame is resized? 调整框架大小时,如何获取要调整的元素的宽度和高度? Here is my layout: 这是我的布局:

Stage stage = new Stage();
stage.setTitle("Hello World");
final Group root = new Group();
Scene scene = new Scene(root);

BorderPane border = new BorderPane();
border.setPrefWidth(stage.getWidth());
border.setPrefHeight(stage.getHeight());

HBox outerHBox = new HBox();
border.setCenter(outerHBox);
root.getChildren().add(border);
stage.setScene(scene);

After some more research I found where this has been done before http://java.dzone.com/articles/setting-stage-javafx-sdk but it is in an older FX (very different from JavaFX 2). 经过更多研究后,我发现在http://java.dzone.com/articles/setting-stage-javafx-sdk之前已完成此操作,但是它在较旧的FX中(与JavaFX 2完全不同)。 I am having trouble translating it. 我在翻译时遇到问题。 Looks like I should be using binding? 看起来我应该使用绑定? I've never used binding before and I've barely used FX. 我以前从未使用过绑定,也几乎没有使用过FX。

Whats the best way to accomplish this? 最好的方法是什么?

Not all Node classes enable resizing. 并非所有Node类都支持调整大小。 The Group class is one of these. Group类是其中之一。 You'll understand this when calling isResizable() on your root object. root对象上调用isResizable()时,您会理解这一点。 Use instead a subclass of Region eg BorderPane as your root . 改为使用Region的子类(例如BorderPane作为root

Stage stage = new Stage();
stage.setTitle("Hello World");

final BorderPane border = new BorderPane();
Scene scene = new Scene(border);

Button button = new Button("test");

HBox outerHBox = new HBox();
outerHBox.getChildren().add(button);
outerHBox.setAlignment(Pos.CENTER);

border.setCenter(outerHBox);

stage.setScene(scene);

Your example should work, now. 您的示例现在应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM