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

时间:2018-05-30 07:20:04

标签: 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>

此代码只能提供此输出: enter image description here 如何在不更改字体属性的情况下显示按钮中的所有文本,如下所示: enter image description here 我的按钮的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;

}

2 个答案:

答案 0 :(得分: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;
}

enter image description here

修改

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

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

但是为了清晰起见,这里建议使用css样式表来做这件事,也因为你已经有了这个。

答案 1 :(得分:1)

尝试将wrapText="true"添加到您的按钮,例如
<Button text = "Explore Catalogue" wrapText="true" ... />

或点击此处:JavaFX Button with multiple text lines