在 setHgrow 的上下文中设置内容对齐
Set content align in context of setHgrow
我正在使用 JavaFX-8 构建应用程序。我正在尝试建立一个菜单。每个菜单项都是一个 ToggleButton
所有 ToggleButtons
应该具有相同的宽度。所以我使用了以下代码片段:
VBox vbox = new VBox();
ToggleButton toggleButton = new ToggleButton("MyText");
HBox hBox = new HBox();
hBox.getChildren().add(toggleButton);
HBox.setHgrow(toggleButton, Priority.ALWAYS);
toggleButton.setMaxWidth(Double.MAX_VALUE);
[...]
vbox.getChildren().add(hBox,hbox2,hbox3,...);
结果:
现在每个项目的宽度相同,但文本对齐现在似乎居中。我试图通过 toggleButton.setContentDisplay(ContentDisplay.LEFT)
和 toggleButton.setAlignment(Pos.CENTER_LEFT)
将对齐方式更改为 LEFT
但没有成功。还有其他方法可以解决这个问题吗?
更新:
我试了 toggleButton.setTextAlignment(TextAlignment.LEFT);
得到了和以前一样的结果。
ToggleButton toggleButton = new ToggleButton("MyText");
HBox hBox = new HBox();
hBox.getChildren().add(toggleButton);
HBox.setHgrow(toggleButton, Priority.ALWAYS);
toggleButton.setMaxWidth(Double.MAX_VALUE);
toggleButton.setTextAlignment(TextAlignment.LEFT);//set text align
参见 JavaDoc ToggleButton :https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ToggleButton.html
和 TextAlignment http://download.java.net/jdk9/jfxdocs/javafx/scene/text/TextAlignment.html#LEFT
选项二
使用 JavaFX 样式 sheet
.toggle-button1{
-fx-font: 30 arial;
-fx-text-alignment: left;
}
在Java
scene.getStylesheets().add("myStyle.css");//load css
toggleButton.getStyleClass().add("toggle-button1");// assign style
在 Labeled
及其子类中有多种控制对齐的机制:
这里的alignmentProperty()就是你想要的。根据文档,它:
Specifies how the text and graphic within the Labeled should be
aligned when there is empty space within the Labeled.
所以你只需要
toggleButton.setAlignment(Pos.CENTER_LEFT);
Specifies the behavior for lines of text when text is multiline Unlike
contentDisplayProperty() which affects the graphic and text, this
setting only affects multiple lines of text relative to the text
bounds.
换句话说,如果您有多行文本,您可以将整个文本视为占据一个矩形,该矩形的高度以所有行为界,宽度以最宽的行为界。 textAlignment
属性 指定 每行 如何在该矩形内对齐。这不适用于您的情况,因为您只有一行文本。
最后是contentDisplayProperty()
。这个
Specifies the positioning of the graphic relative to the text.
只有在按钮中同时显示文本和图形时,才会有所不同。将其设置为 ContentDisplay.LEFT
指定图形应放置在文本的左侧,例如。
我正在使用 JavaFX-8 构建应用程序。我正在尝试建立一个菜单。每个菜单项都是一个 ToggleButton
所有 ToggleButtons
应该具有相同的宽度。所以我使用了以下代码片段:
VBox vbox = new VBox();
ToggleButton toggleButton = new ToggleButton("MyText");
HBox hBox = new HBox();
hBox.getChildren().add(toggleButton);
HBox.setHgrow(toggleButton, Priority.ALWAYS);
toggleButton.setMaxWidth(Double.MAX_VALUE);
[...]
vbox.getChildren().add(hBox,hbox2,hbox3,...);
结果:
现在每个项目的宽度相同,但文本对齐现在似乎居中。我试图通过 toggleButton.setContentDisplay(ContentDisplay.LEFT)
和 toggleButton.setAlignment(Pos.CENTER_LEFT)
将对齐方式更改为 LEFT
但没有成功。还有其他方法可以解决这个问题吗?
更新:
我试了 toggleButton.setTextAlignment(TextAlignment.LEFT);
得到了和以前一样的结果。
ToggleButton toggleButton = new ToggleButton("MyText");
HBox hBox = new HBox();
hBox.getChildren().add(toggleButton);
HBox.setHgrow(toggleButton, Priority.ALWAYS);
toggleButton.setMaxWidth(Double.MAX_VALUE);
toggleButton.setTextAlignment(TextAlignment.LEFT);//set text align
参见 JavaDoc ToggleButton :https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ToggleButton.html 和 TextAlignment http://download.java.net/jdk9/jfxdocs/javafx/scene/text/TextAlignment.html#LEFT
选项二
使用 JavaFX 样式 sheet
.toggle-button1{
-fx-font: 30 arial;
-fx-text-alignment: left;
}
在Java
scene.getStylesheets().add("myStyle.css");//load css
toggleButton.getStyleClass().add("toggle-button1");// assign style
在 Labeled
及其子类中有多种控制对齐的机制:
这里的alignmentProperty()就是你想要的。根据文档,它:
Specifies how the text and graphic within the Labeled should be aligned when there is empty space within the Labeled.
所以你只需要
toggleButton.setAlignment(Pos.CENTER_LEFT);
Specifies the behavior for lines of text when text is multiline Unlike contentDisplayProperty() which affects the graphic and text, this setting only affects multiple lines of text relative to the text bounds.
换句话说,如果您有多行文本,您可以将整个文本视为占据一个矩形,该矩形的高度以所有行为界,宽度以最宽的行为界。 textAlignment
属性 指定 每行 如何在该矩形内对齐。这不适用于您的情况,因为您只有一行文本。
最后是contentDisplayProperty()
。这个
Specifies the positioning of the graphic relative to the text.
只有在按钮中同时显示文本和图形时,才会有所不同。将其设置为 ContentDisplay.LEFT
指定图形应放置在文本的左侧,例如。