GridPane中的按钮,prefWidth =" Infinity"打破JavaFX [英] Buttons in GridPane with prefWidth="Infinity" break JavaFX

查看:960
本文介绍了GridPane中的按钮,prefWidth =" Infinity"打破JavaFX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用JavaFX,所以请原谅这是否是一个愚蠢的错误。

I am just starting with JavaFX so please excuse if this is a dumb mistake to make.

我有一个 GridPane 我想用它们两个按钮紧挨着窗户的整个宽度。所以我创建了一个 GridPane ,其中包含一行和两个列,并添加到我的两个按钮中。

I have a GridPane which I want to use to have two buttons next to eachother stretch over the whole width of the window. So I created a GridPane with one row and two collumns and added in my two buttons.

代码:

<GridPane id="GridPane" alignment="TOP_LEFT" disable="false" gridLinesVisible="false" hgap="5.0">
  <children>
     <Button mnemonicParsing="false"  styleClass="add-button" text="Save" GridPane.columnIndex="0" GridPane.rowIndex="0" />
     <Button mnemonicParsing="false"  text="Discard" GridPane.columnIndex="1" GridPane.rowIndex="0" />
  </children>
  <columnConstraints>
     <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
     <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
  </columnConstraints>
  <rowConstraints>
     <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
  </rowConstraints>
</GridPane>

但现在Button只占用了他们必要的空间。所以我向他们两个添加了 prefWidth =Infinity。这导致 Secene Builder 1.1 立即冻结。我听说场景构建器是,并且可能是有缺陷的,所以我手动将其编码到FXML并尝试运行该程序。程序启动但不打开窗口。

But now the Button only take up their necessary space. So I added prefWidth="Infinity" to both of them. This results in the Secene Builder 1.1 instantly freezing. I heard that the Scene Builders were and probably are buggy so I coded it into the FXML by hand and tried to run the program. The program starts but does not open a window.

使用 jvisualvm 检查正在运行的进程告诉我JavaFX应用程序线程正在分配大量空间(对于数以百万计的 ArrayList 即时收集垃圾。它分配大约1.2 GB / s。相比之下,当我尝试用它打开文件时,Scene Builder似乎什么也没做,因为它的使用堆非常常常在16MB左右(它也没有打开一个窗口)。

Inspection of the running process with jvisualvm tells me that the JavaFX Application Thread is allocating huge amounts of space (for millions of ArrayLists that get instantly garbage collected). It allocates about 1.2 GB/s. The Scene Builder in contrast seems to do about nothing when I try to open the file with it as its used heap is pretty constant at about 16MB (it also does not open a window).

即使 prefWidth 仅添加在一个按钮上,也会发生错误。如果我在两个按钮上使用例如 prefWidth =100000000,我会得到所需的结果,但我觉得这实际上是错误的编码。

Even if the prefWidth is only added on one Button the error happens. If I use for example prefWidth="100000000" on both buttons I get the desired result but I feel that this is realy bad coding.

所以我的问题是:


  • 这是JavaFX的错误吗?

  • 如何以更好的方式获得我想要的结果?

推荐答案

你应该使用maxWidth和不是prefWidth。 JavaFX正在尝试将MAX值计算为按钮的初始大小,因为它是首选大小。最大尺寸只是用于比较实际尺寸的参数(在某些情况下,将尺寸设置为最小值(实际尺寸不够大:将其增大到最小尺寸)或最大值(实际尺寸太大) :减少到最大尺寸)),它可能是你需要的;带有'无穷大'值的maxWidth / maxHeight意味着你不限制你的小部件的增长。

You should use maxWidth and not prefWidth. JavaFX is trying to compute the MAX value as the initial size for your button as it is the preferred size. Max size is only a parameter that is used to compare the actual size to (and in some cases, setting the size to a min (actual size is not big enough: augment it to min size) or max value (actual size is too big: diminish it to max size)), it is probably what you need ; maxWidth / maxHeight with 'infinity' value means you don't limit the growth of your widget.

默认情况下,max size等于prefSize,这就是为什么你有修改它以拥有正确的HGROW / VGROW。你在某些方面是正确的:你需要修改最大尺寸,但不要改变pref尺寸。

By default, max size is equal to prefSize, that is why you have to modify it to have a correct HGROW / VGROW. You were correct on some point: you need to modify the max size, but do not change the pref size.

这样的事情应该有效:

<Button fx:id="testButton"
        text="Test"
        maxWidth="Infinity"
        maxHeight="Infinity"
        GridPane.hgrow="ALWAYS"
        GridPane.vgrow="ALWAYS"
        GridPane.rowIndex="0"
        GridPane.columnIndex="0"/>

这篇关于GridPane中的按钮,prefWidth =&quot; Infinity&quot;打破JavaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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