javaFX 2.0将组件设置为直接父级的全宽和高度

Far*_*san 13 java javafx-2

如何TextArea获取父窗格的完整宽度和高度.

我试过这个:

TextArea textArea = new TextArea();
textArea.setScaleX( 100 );
textArea.setScaleY( 100 );
Run Code Online (Sandbox Code Playgroud)

但parent.setTop(...)覆盖了顶部通道中定义的元素.
减少scaleY没有影响.

我还需要做些什么才能做到这一点?

谢谢

kyl*_*yre 27

MAX_VALUE解决方案有点hacky,可能会导致性能问题.此外,答案可能取决于您的父容器是什么.无论如何,更好的方法是这样:

textArea.prefWidthProperty().bind(<parentControl>.prefWidthProperty());
textArea.prefHeightProperty().bind(<parentConrol>.prefHeightProperty());
Run Code Online (Sandbox Code Playgroud)

您可能还希望将首选属性绑定到实际属性,特别是如果父级使用的是计算维度而不是显式维度:

textArea.prefWidthProperty().bind(<parentControl>.widthProperty());
textArea.prefHeightProperty().bind(<parentConrol>.heightProperty());
Run Code Online (Sandbox Code Playgroud)

通过覆盖父容器的layoutChildren()方法并调用,也可以在不使用绑定的情况下执行此操作

textArea.resize(getWidth(), getHeight());
Run Code Online (Sandbox Code Playgroud)

别忘了给super.layoutChildren()打电话...


Far*_*san 11

解决了这个问题

textArea.setPrefSize( Double.MAX_VALUE, Double.MAX_VALUE );
Run Code Online (Sandbox Code Playgroud)

  • 不,这行不通。也许它会意外地起作用,但可以肯定这不是“正确的方法”。 (4认同)

pmo*_*ule 5

您通过将做到这一点TextArea的BorderPane。

Stage stage = new Stage();
stage.setTitle("Resizing TextArea");

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

TextArea textArea = new TextArea();
textArea.setStyle("-fx-background-color: #aabbcc;");

border.setCenter(textArea);

primaryStage.setScene(scene);
primaryStage.setVisible(true);
Run Code Online (Sandbox Code Playgroud)

您也可以将它放在 anHBox或 a 中VBox。然后调整大小仅限于水平/垂直方向。不确定这是否是一个问题。