简体   繁体   English

如何在 JavaFX 中的按钮中显示文本?

[英]How can i show text in a Button in JavaFX?

i'm trying to display some text on those 3 buttons on the top-left corner but somehow it's not possible...我试图在左上角的那 3 个按钮上显示一些文本,但不知何故这是不可能的......

Here's image so you can see where the buttons are:这是图像,因此您可以看到按钮的位置:

在此处输入图像描述

What i've already tried:我已经尝试过的:

  • Adding wraptext="true"添加 wraptext="true"
  • setting textOverrun="CLIP" But it doesn't work in both cases!设置 textOverrun="CLIP" 但它在这两种情况下都不起作用!

Here's the FXML snippet:这是 FXML 片段:

<HBox fx:id="buttons_group" alignment="CENTER_LEFT" prefHeight="22.0" spacing="7" style="-fx-padding: 0 0 0 8" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.5" AnchorPane.topAnchor="0.0">

                  <Button fx:id="closebtn" text="×" textFill="black" onMouseClicked="#onClickCloseBtn" style="-fx-background-radius: 50; -fx-min-height: 9; -fx-min-width: 9; -fx-max-height: 9; -fx-max-width: 9; -fx-background-color: #FF453A" />
                  <Button fx:id="minimizebtn" text="-" textFill="black" onMouseClicked="#onClickMinimizeBtn" style="-fx-background-radius: 50; -fx-min-height: 9; -fx-min-width: 9; -fx-max-height: 9; -fx-max-width: 9; -fx-background-color: #FFD60A" />
                  <Button fx:id="maximizebtn" text="□" textFill="black" onMouseClicked="#onClickMaximizeBtn" style="-fx-background-radius: 50; -fx-min-height: 9; -fx-min-width: 9; -fx-max-height: 9; -fx-max-width: 9; -fx-background-color: #32D74B" />
        </HBox

Thanks!谢谢!

Set negative padding to your buttons.为您的按钮设置负填充。

You might also want to decrease font size as the default is 12px, while your buttons are 9px in size.您可能还想减小字体大小,因为默认值为 12px,而您的按钮大小为 9px。

Centering is not perfect with such small buttons though...这么小的按钮居中并不完美......

Here's an example done in SceneBuilder.这是在 SceneBuilder 中完成的示例。

  <HBox alignment="CENTER_LEFT" prefHeight="22.0" spacing="7.0" style="-fx-background-color: #333;">
     <children>
        <Button maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="9.0" prefWidth="9.0" style="-fx-background-radius: 4.5; -fx-background-color: #FF453A; -fx-padding: -100;" text="x">
           <font>
              <Font size="10.0" />
           </font>
        </Button>
        <Button maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="9.0" prefWidth="9.0" style="-fx-background-radius: 4.5; -fx-background-color: #FFD60A; -fx-padding: -100;" text="-">
           <font>
              <Font size="10.0" />
           </font>
        </Button>
        <Button maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" prefHeight="9.0" prefWidth="9.0" style="-fx-background-radius: 4.5; -fx-background-color: #32D74B; -fx-padding: -100;" text="□">
           <font>
              <Font size="10.0" />
           </font>
        </Button>
     </children>
     <padding>
        <Insets bottom="7.0" left="7.0" right="7.0" top="7.0" />
     </padding>
  </HBox>

Result:结果: 结果在 SceneBuilder 中可视化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM