【问题标题】:How to Display all the text in Buttons in JavaFx如何在 JavaFx 中显示按钮中的所有文本
【发布时间】:2018-11-08 22:48:44
【问题描述】:

所以我试图在 JavaFX 中显示所有文本,它应该显示字体中的所有文本: 这是我的 javafx 代码

<HBox> 
<GridPane xmlns:fx= "http://javafx.com/fxml" hgap = "10" vgap = "0">

<Button text = "Explore Catalogue" alignment = "center" styleClass = "largeButton" GridPane.columnIndex="0" GridPane.rowIndex="2"/>
<Button text = "Customer Record" styleClass = "largeButton" GridPane.columnIndex="1" GridPane.rowIndex="2"/>
<Button text = "Top-up Account" styleClass = "largeButton" GridPane.columnIndex="2" GridPane.rowIndex="2"/>
<Button text = "Favourite Movies" styleClass = "largeButton" GridPane.columnIndex="3" GridPane.rowIndex="2"/>



</GridPane>

这段代码只能给出这个输出: 如何在不更改应如下所示的字体属性的情况下显示按钮中的所有文本: 我的按钮css代码是:

.largeButton {
-fx-font-family: "Verdana";
-fx-pref-width: 200px;
-fx-pref-height: 200px;
-fx-font-size: 28px;
-fx-background-color: white;
-fx-text-fill: #4d4b44;
-fx-border-color: #dedede;

}

【问题讨论】:

  • 你试过-fx-wrap-text: true; 吗?

标签: java javafx


【解决方案1】:

通过在您的 css 样式表中添加 -fx-wrap-text: true;,它就可以解决问题。这个 css 属性继承自 Labeled 并能够巧妙地(令人惊讶地)包装文本(首先是空格)。

.largeButton {
    -fx-font-family: "Verdana";
    -fx-pref-width: 200px;
    -fx-pref-height: 200px;
    -fx-font-size: 28px;
    -fx-background-color: white;
    -fx-text-fill: #4d4b44;
    -fx-border-color: #dedede;
    -fx-wrap-text : true;
}

编辑

这也可以通过带有myLargeButton.setWrapText(true) 的代码来完成,或者通过添加wrapText="true" 直接在您的FXML 文件中添加它,这将为您提供:

<Button text = "Favourite Movies" wrapText="true" styleClass = "largeButton" GridPane.columnIndex="3" GridPane.rowIndex="2"/>

但是为了清楚起见,这里建议使用 css 样式表执行此操作,也因为您已经有了一个。

【讨论】:

    【解决方案2】:

    尝试将wrapText="true" 添加到您的按钮中,例如
    &lt;Button text = "Explore Catalogue" wrapText="true" ... /&gt;

    或在这里查看:JavaFX Button with multiple text lines

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-17
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      相关资源
      最近更新 更多