JavaFX:如何使我的自定义组件使用父布局中的所有可用空间? [英] JavaFX: How to make my custom component use all available space from parent layout?

查看:679
本文介绍了JavaFX:如何使我的自定义组件使用父布局中的所有可用空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为javafx创建一个自定义组件,所以我让我的类扩展javafx.scene.control.Control,并在构造函数上绘制它的内容。

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.

我的怀疑是:如何让它成长以填充其父布局上的所有可用空间?就像我放置TextArea一样......

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...

这是我正在做的缩小的例子:

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();
}

在此示例中,TextArea将获得布局为其提供的所有空间。我希望我的组件也一样!

On this example the TextArea will get all the space the layout provide for it. I want my component do the same!

我已经尝试过的所有操作:
- 更改属性setMaxWidth / Height,setPrefWidth / Height,setMinWidth / Height; (当我设置MinValues时,它总是使用这些值绘制但不再更新width值)
- 尝试使用Double.MAX_VALUE强制更大的尺寸然后布局privides;
一切都不起作用:(我一定是在误解......
- 我也哭了一点,但即使这样也无济于事='(

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 ='(

也许是一个线索,我在下面放了一个这样的监听器,每当我调整我的应用程序/阶段时,它都会更新TextArea。但是当我添加这个监听器时,我的组件永远不会得到更新......

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 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...

我已经尝试了Google,但似乎JavaFX的文档太小而且肤浅,而且很多都是基于JavaFX脚本...

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...

我也接受任何更好的教程,就像甲骨文提供的那样......

I also accepts any tutorial better them the one offered by Oracle...

在任何帮助之前感谢...我写的东西很多... = O

Thanks in advance of any help... I thing I wrote to much... =O

推荐答案

我找到了什么我错误...

I found what I was mistaking...

要使组件与其父组成长,您需要做的就是在maxwidth和/或maxheight上定义它的最大值为double(如下所示),并将minwidth / height和prefwidth / height设为默认值。

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.

this.setMaxWidth(Double.MAX_VALUE);
this.setMaxHeight(Double.MAX_VALUE);

使用此配置,当父级更改可用空间时,它将使用最终方法setWidth()/ setHeight ()更新组件可以使用的宽度/高度。因此,由于这些方法是最终的,因此无法覆盖它以便在发生更改时知道。您需要添加如下所示的changelistener,以便在需要重新绘制组件时收到通知。

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.

t.widthProperty().addListener(new ChangeListener() {
  public void changed(ObservableValue observable, Object oldValue, Object newValue) {
    System.out.println(oldValue + "|" + newValue);
  }
});

这篇关于JavaFX:如何使我的自定义组件使用父布局中的所有可用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆