在 JavaFX 中,当组件的 minWidth
设置为 USE_COMPUTED_SIZE
时,组件的最小宽度将根据其内容计算。
虽然我已成功地将其用于许多组件,但对于 Button
来说,它似乎没有按预期工作:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<Button mnemonicParsing="false" text="This should always be visible" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" />
所以我测试了将 minWidth
设置为 300 时会发生什么:
我做错了什么?
更新
为了测试舞台是否忽略按钮的 minWidth
,我将按钮包装到 HBox 中,并在按钮周围添加了边距。
将 minWidth
设置为 USE_COMPUTED_SIZE
后,结果如下:
当我将 minWidth
设置为 200 时,我得到:
这很好 - 除了我需要计算大小。
最佳答案
JavaFX API 文档说:
Defaults to the
USE_COMPUTED_SIZE
flag, which means thatminWidth(forHeight)
will return the region's internally computed minimum width.Setting this value to the
USE_PREF_SIZE
flag will causeminWidth(forHeight)
to return the region's preferred width, enabling applications to easily restrict the resizability of the region.
关于JavaFX 按钮 minWidth 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53114841/