JavaFX 按钮 minWidth 被忽略

标签 java javafx

在 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" />

enter image description here enter image description here

所以我测试了将 minWidth 设置为 300 时会发生什么:

enter image description here enter image description here

我做错了什么?

更新

为了测试舞台是否忽略按钮的 minWidth,我将按钮包装到 HBox 中,并在按钮周围添加了边距。 将 minWidth 设置为 USE_COMPUTED_SIZE 后,结果如下:

enter image description here enter image description here

当我将 minWidth 设置为 200 时,我得到:

enter image description here

这很好 - 除了我需要计算大小。

最佳答案

JavaFX API 文档说:

Defaults to the USE_COMPUTED_SIZE flag, which means that minWidth(forHeight) will return the region's internally computed minimum width.

Setting this value to the USE_PREF_SIZE flag will cause minWidth(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/

相关文章:

java - CriteriaBuilder 使用自定义条件连接两个表

java - LLVM 中类型的大小

java - Swing组件添加到ScrollPane(JavaFX)

Java:如何从主类调用GUI类(没有Swing/JFrame)

JavaFX:如何在 TreeView 中添加复选框和按钮?

JavaFX CSS 在运行时更改自定义样式类的属性

java - 扩展 Play 框架的 GenericModel

java - 使所有元素(包括 2 个 ListView )在 JavaFX 中一起滚动

java - JMenuBar 导致内容 Pane 重新绘制不正确

JavaFX.登录页面链接到下一页