如何在JavaFx中显示按钮中的所有文本

Sam*_*rat 5 java javafx

所以我试图显示 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>
Run Code Online (Sandbox Code Playgroud)

此代码只能给出以下输出: 在此输入图像描述 如何在不更改字体属性的情况下显示按钮中的所有文本,字体属性应如下所示: 在此输入图像描述 我的按钮的 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;
Run Code Online (Sandbox Code Playgroud)

}

Pag*_*gbo 2

通过添加-fx-wrap-text: true;你的 CSS 样式表,它就能达到目的。这个 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;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

编辑

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

<Button text = "Favourite Movies" wrapText="true" styleClass = "largeButton" GridPane.columnIndex="3" GridPane.rowIndex="2"/>
Run Code Online (Sandbox Code Playgroud)

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