JavaFX:如何让我的自定义组件使用父布局中的所有可用空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9816099/
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
JavaFX: How to make my custom component use all available space from parent layout?
提问by Rodrigo Leit?o
I′m trying to make a custom component to javafx, so I make my class extends javafx.scene.control.Control and on constructor it draws it′s contents.
我正在尝试为 javafx 创建一个自定义组件,所以我让我的类扩展 javafx.scene.control.Control 并在构造函数上绘制它的内容。
My doubts are: how do I make it "grow" to fill all the space available on its parent layout? Like when I place a TextArea instead...
我的疑问是:如何让它“增长”以填充其父布局上的所有可用空间?就像当我放置一个 TextArea 时......
This is an shrink example of what I′m doing:
这是我正在做的缩小示例:
@Override
public void start(Stage primarystage) throws Exception {
BorderPane bpane = new BorderPane();
mainscene = new Scene(bpane, 800, 600);
((BorderPane) this.mainscene.getRoot()).setCenter(t);
primarystage.setScene(mainscene);
primarystage.show();
}
On this example the TextArea will get all the space the layout provide for it. I want my component do the same!
在此示例中,TextArea 将获得布局为其提供的所有空间。我希望我的组件也这样做!
All I already tried to do: - Change the properties setMaxWidth/Height, setPrefWidth/Height, setMinWidth/Height; (when I set the MinValues, it always use this values to draw but don′t update the "width" value anymore) - Tried to use Double.MAX_VALUE to force a bigger size then layout privides; Everything didn′t work :( I must be mistaking somehow... - I cried a little too, but even this make nothing work ='(
我已经尝试做的所有事情: - 更改属性 setMaxWidth/Height、setPrefWidth/Height、setMinWidth/Height;(当我设置 MinValues 时,它总是使用这个值来绘制但不再更新“宽度”值) - 尝试使用 Double.MAX_VALUE 来强制更大的尺寸然后布局特权;一切都不起作用:(我一定是搞错了...... - 我也哭了一点,但即使这样也无济于事='(
Maybe a clue is, I put a listener like this below, and everytime I resize my application/stage it updates the TextArea. But when I add this listener do my component it never get update...
也许一个线索是,我在下面放置了一个这样的侦听器,每次我调整应用程序/阶段的大小时,它都会更新 TextArea。但是当我添加这个监听器时,我的组件永远不会更新......
TextArea t = new TextArea("teste");
t.widthProperty().addListener(new ChangeListener() {
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
System.out.println(oldValue + "|" + newValue);
}
});
Probably I′m passing by some properties or forgetting something, I don′t know...
可能是我路过一些属性或者忘记了一些东西,我不知道......
I already tried Google, but seems the documentation of JavaFX is too small and superficial, and a lot of them is based on JavaFX script...
我已经尝试过谷歌,但似乎JavaFX的文档太小而且肤浅,而且很多都是基于JavaFX脚本的......
I also accepts any tutorial better them the one offered by Oracle...
我也接受任何更好的教程,比如 Oracle 提供的教程...
Thanks in advance of any help... I thing I wrote to much... =O
提前感谢任何帮助......我写的东西很多...... =O
采纳答案by Rodrigo Leit?o
I found what I was mistaking...
我发现我错了...
To make a component grow with it′s parent, all you need to do is define on maxwidth and/or maxheight it max value of double (like below), and let minwidth/height and prefwidth/height with it′s default values.
为了让一个组件随着它的父组件增长,你需要做的就是在 maxwidth 和/或 maxheight 上定义它的最大值 double(如下所示),并让 minwidth/height 和 prefwidth/height 使用它的默认值。
this.setMaxWidth(Double.MAX_VALUE);
this.setMaxHeight(Double.MAX_VALUE);
With this configuration when the parent changes the available space it will use the final method setWidth()/setHeight() to update the width/height your component can use. So, since these methods are final you can′t override it to "know" when a change occurs. You′ll need to add a changelistener like below to be notified when you need to repaint your component.
使用此配置,当父级更改可用空间时,它将使用最终方法 setWidth()/setHeight() 更新组件可以使用的宽度/高度。因此,由于这些方法是最终的,因此您无法覆盖它以“知道”何时发生更改。您需要添加如下所示的更改侦听器,以便在您需要重新绘制组件时收到通知。
t.widthProperty().addListener(new ChangeListener() {
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
System.out.println(oldValue + "|" + newValue);
}
});
回答by gergang
I found (for Java 8) - that custom controls only resize properly inside a parent node when they belong to GridPane, HBox, VBox or, preferably, to StackPane. The Oracle CustomControl example uses VBox. I could′nt get resizing to work with an AnchorPane as top node of my custom control, not even when explicitly setting maxWidth & maxHeight
我发现(对于 Java 8)- 当自定义控件属于 GridPane、HBox、VBox 或最好属于 StackPane 时,它们只会在父节点内正确调整大小。Oracle CustomControl 示例使用 VBox。我无法调整大小以使用 AnchorPane 作为我的自定义控件的顶部节点,即使在显式设置 maxWidth 和 maxHeight 时也不行
So the amended CustomControl.fxml is like this:
所以修改后的CustomControl.fxml是这样的:
?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. -->
<?import java.lang.*?>
<?import javafx.collections.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root type="javafx.scene.layout.StackPane" xmlns:fx="http://javafx.com/fxml">
(and here inside the StackPane one can place the actual control, e.g. the
Anchor Pane with child nodes from ScreenBuilder -
works without maxWidth or maxHeight)
</fx:root>
and CustomControl.java extends StackPane (instead of VBox)
和 CustomControl.java 扩展了 StackPane(而不是 VBox)