带有多个文本行的JavaFX Button
我需要在屏幕上创建一个工具栏,它有多个按钮,每个按钮必须有多行文本。 例如:
我查看了互联网和StackOverflow,但我找不到任何显示如何在JavaFX中执行此操作的内容。 我正在使用JavaFX 8。
有人可以帮帮我吗?
TKS
您也可以使用wrapTextProperty
。 但您必须将工具栏高度设置为大于预期的按钮高度。
Button btn = new Button(); btn.wrapTextProperty().setValue(true); // or btn.setWrapText(true); btn.setText("Some looooooooooooong text");
或者,如果您想确定行应该包裹的确切位置,您可以这样:
Button btn = new Button(); btn.setText("Line1\n Line2\n Line3");
最后的方法可以在不改变工具栏高度的情
我解决了这个问题,包括我的按钮内的VBox,然后在VBox中包含几个标签。 喜欢这个:
结果是:
如果有更优雅的方式来获得相同的结果,请告诉我。 谢谢。
在按钮文本属性中选择“切换到多行模式
“
我的解决方案与OP给出的解决方案几乎相同,但不是Label
使用Text
因此更改按钮大小更灵活,因为它将根据需要使用尽可能多的行。 如果需要,也可以设置包装宽度,以定义宽度约束。
@Override public void start(Stage primaryStage) { Button btn = new Button(); ImageView imageView = new ImageView(new Image(getClass().getResource().toExternalForm())); Text text=new Text("Some long text that may be line wrapped"); text.setWrappingWidth(100); VBox vBox = new VBox(5, imageView,text); vBox.setAlignment(Pos.CENTER); btn.setGraphic(vBox); btn.setContentDisplay(ContentDisplay.GRAPHIC_ONLY); Scene scene = new Scene(new StackPane(btn), 300, 250); primaryStage.setScene(scene); primaryStage.show(); }
Label label= new Label("your long text here"); label.setStyle("-fx-max-width : 180px); label.setWrapText(true);
从sobolev的回复中,你可以做到:
Button btn = new Button(); btn.setText("Line1\n Line2\n Line3"); button.textAlignmentProperty().set(TextAlignment.CENTER);
这将创建3行文本并将它们分配到按钮的中心。