如何在 JavaFX 的按钮中显示文本?
How can i show text in a Button in JavaFX?
我正在尝试在左上角的那 3 个按钮上显示一些文本,但不知怎么的,这是不可能的...
这是图片,您可以看到按钮的位置:
我已经尝试过的:
- 添加 wraptext="true"
- 设置 textOverrun="CLIP"
但它在这两种情况下都不起作用!
这是 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
谢谢!
为您的按钮设置负边距。
您可能还想减小字体大小,因为默认值为 12px,而您的按钮大小为 9px。
虽然如此小的按钮居中并不完美...
这是在 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>
结果:
我正在尝试在左上角的那 3 个按钮上显示一些文本,但不知怎么的,这是不可能的...
这是图片,您可以看到按钮的位置:
我已经尝试过的:
- 添加 wraptext="true"
- 设置 textOverrun="CLIP" 但它在这两种情况下都不起作用!
这是 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
谢谢!
为您的按钮设置负边距。
您可能还想减小字体大小,因为默认值为 12px,而您的按钮大小为 9px。
虽然如此小的按钮居中并不完美...
这是在 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>
结果: