簡體   English   中英

javaFX 2.0將組件設置為直接父級的全寬和高度

[英]javaFX 2.0 set component to full width and height of immediate parent

如何使TextArea獲取父窗格的完整寬度和高度。

我試過這個:

TextArea textArea = new TextArea();
textArea.setScaleX( 100 );
textArea.setScaleY( 100 );

但涵蓋了通過parent.setTop(...)在頂部定義的元素。
減少scaleY沒有任何影響。

我還需要做些什么才能做到這一點?

謝謝

MAX_VALUE解決方案有點hacky,可能會導致性能問題。 此外,答案可能取決於您的父容器是什么。 無論如何,更好的方法是這樣:

textArea.prefWidthProperty().bind(<parentControl>.prefWidthProperty());
textArea.prefHeightProperty().bind(<parentConrol>.prefHeightProperty());

您可能還希望將首選屬性綁定到實際屬性,特別是如果父級使用的是計算維度而不是顯式維度:

textArea.prefWidthProperty().bind(<parentControl>.widthProperty());
textArea.prefHeightProperty().bind(<parentConrol>.heightProperty());

通過覆蓋父容器的layoutChildren()方法並調用,也可以在不使用綁定的情況下執行此操作

textArea.resize(getWidth(), getHeight());

別忘了給super.layoutChildren()打電話...

解決了這個問題

textArea.setPrefSize( Double.MAX_VALUE, Double.MAX_VALUE );

您可以通過將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);

您也可以將它放在HBoxVBox 然后調整大小限於水平/垂直方向。 不確定這是否是一個問題。

<TextArea style="-fx-pref-height: 10px;"/>

你可以通過創建一個css文件來做到這一點。

textarea
{
width://your width
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM