java 带有多个文本行的 JavaFX 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27295505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:34:23  来源:igfitidea点击:

JavaFX Button with multiple text lines

javabuttonjavafxjavafx-8multiline

提问by henriqueor

I need to create a toolbar in my screen that will have multiple buttons, and each button must have multiple lines of Text. For example:

我需要在我的屏幕中创建一个有多个按钮的工具栏,每个按钮必须有多行文本。例如:

Button with multiple lines

多行按钮

I looked over the internet and StackOverflow but I couldn't find anything showing how to do this in JavaFX. I'm using JavaFX 8.

我查看了互联网和 StackOverflow,但找不到任何显示如何在 JavaFX 中执行此操作的内容。我正在使用 JavaFX 8。

Someone could help me, please?

有人可以帮帮我好吗?

Tks

塔克斯

采纳答案by henriqueor

I resolved this problem including a VBox inside my button, and then including several Labels inside the VBox. Like this:

我解决了这个问题,在我的按钮中包含了一个 VBox,然后在 VBox 中包含了几个标签。像这样:

enter image description here

在此处输入图片说明

The result is:

结果是:

enter image description here

在此处输入图片说明

If there is a more elegant way to have the same result, please, let me know. Thank you.

如果有更优雅的方法来获得相同的结果,请告诉我。谢谢你。

回答by soboliev.stp

Also you can use the wrapTextProperty. But you have to set toolbar height greater than expected button height.

您也可以使用wrapTextProperty. 但是您必须将工具栏高度设置为大于预期的按钮高度。

Button btn = new Button();
btn.wrapTextProperty().setValue(true);
// or btn.setWrapText(true);
btn.setText("Some looooooooooooong text");

Or if you want to determine exactly where the line should be wrapped, you can go this way:

或者,如果您想确定该行的确切位置,您可以这样做:

Button btn = new Button();
btn.setText("Line1\n Line2\n Line3");

Last way will work without changing toolbar height.

最后一种方法可以在不更改工具栏高度的情况下工作。

回答by Vishrant

In the button text property select "switch to multi-line mode

在按钮文本属性中选择“切换到多行模式

enter image description here"

在此处输入图片说明

回答by Aibek

From sobolev's response, you can do:

从索博列夫的回应中,您可以执行以下操作:

Button btn = new Button();
btn.setText("Line1\n Line2\n Line3");
button.textAlignmentProperty().set(TextAlignment.CENTER);

This will create 3 lines of text and allign them in the center of your button.

这将创建 3 行文本并将它们对齐到按钮的中心。

回答by José Pereda

My solution is pretty much the same as the one given by the OP, but instead of Labeluses Textso it's more flexible to changes in the size of the button, as it will use as many lines as needed. If required, also one can set a wrapping width, to define a width constraint.

我的解决方案与 OP 给出的解决方案几乎相同,但不是Label使用,Text因此更改按钮的大小更灵活,因为它将根据需要使用尽可能多的行。如果需要,还可以设置环绕宽度,以定义宽度约束。

@Override
public void start(Stage primaryStage) {
    Button btn = new Button();
    ImageView imageView = new ImageView(new Image(getClass().getResource(<image>).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();
}

回答by Issam El omri

Label label= new Label("your long text here");
label.setStyle("-fx-max-width : 180px);
label.setWrapText(true);