简体   繁体   English

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

[英]JavaFX: How to display rich text in a button?

In JavaFX-8 ( Edit: I realized I'm using jdk-9.0.1, might be a difference?) , I want a component with the functionality of a Button but where its label can be set as rich text. 在JavaFX-8中编辑:我意识到我正在使用jdk-9.0.1,可能有所不同吗?) ,我想要一个具有Button功能但其标签可以设置为富文本格式的组件。 So far I've tried: 到目前为止,我已经尝试过:

  1. Using a TextFlow alone and with other components (Pane, StackPane) and CSS styling to add background color and border. 单独使用TextFlow以及与其他组件(Pane,StackPane)和CSS样式配合使用,以添加背景颜色和边框。 This did not work because I could not get the text to be centered vertically in the component when I had set the component's preferred width and height. 这没有用,因为在设置组件的首选宽度和高度时,我无法使文本在组件中垂直居中。

  2. The same as above but using vertical padding to center the text. 与上述相同,但使用垂直填充使文本居中。 This did not work because I could not set the height of the button. 这无法工作,因为我无法设置按钮的高度。 If I set a maximum width the text would wrap, but on wrapping, because the padding is constant size, the overall height of the component would increase instead of the text simply taking up more space or being clipped to fit (and appended with "..." like in a Button). 如果设置了最大宽度,则文本将自动换行,但是在换行时,由于填充是恒定大小,因此组件的整体高度会增加,而不是文本会简单地占用更多空间或被剪裁以适合文本(并附加“。等等。”

  3. A button with no text and using its setGraphic method to set its graphic node to a TextFlow of my choosing. 一个没有文本的按钮,并使用其setGraphic方法将其图形节点设置为我选择的TextFlow。 This did not work becuase the TextFlow isn't clipped to fit within the button. 由于TextFlow没有被裁剪为适合按钮的大小,因此无法正常工作。 Instead, if it is to large, it is displayed outside the borders of the button. 而是,如果它太大,则显示在按钮边界之外。 Edit: It does not seem possible to center the graphic vertically either. 编辑: 似乎也不可能将图形垂直居中。 This is only done when the Button has text as well as graphic, but then it does not wrap instead. 仅当Button同时具有文本和图形时才执行此操作,但是它不会自动换行。

  4. I also noticed that a Button does have a TextFlow child, but unfortunately it seems I can't set it as I can only return it using Button's getChildrenUnmodifiable method. 我还注意到Button确实有一个TextFlow子对象,但是不幸的是,我似乎无法设置它,因为只能使用Button的getChildrenUnmodifiable方法将其返回。

Is there some other way to accomplish this, or is there some reasonable way to make one of the approaches I've already tried work? 是否有其他方法可以实现此目的,或者有某种合理的方法可以使我已经尝试过的一种方法起作用?

To summarize, some important things I need: 总结一下,我需要一些重要的事情:

  • Ability to use component like a button. 能够使用按钮之类的组件。
  • Display rich text in the component (with optional vertical centering). 在组件中显示富文本(具有可选的垂直居中)。
  • Easily set and change the rich text or parts of it. 轻松设置和更改RTF文本或其中的一部分。
  • Allow the rich text to be clipped when it does no longer fit. 当富文本不再适合时,可以对其进行剪切。
  • Somehow set component size so it doesn't resize if text becomes large. 以某种方式设置组件的大小,以便在文本变大时不调整大小。

Per request, an MCVE where #3 does not work for me: 根据请求,MCVE在其中#3对我不起作用:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class TestWindow extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox mainLayout = new VBox();

        Text t = new Text(
                "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis blandit dolor non dui porttitor, sit amet commodo ipsum condimentum. Proin quis tincidunt massa.");
        TextFlow a = new TextFlow(t);
        a.setTextAlignment(TextAlignment.CENTER);

        Button b = new Button();
        b.setGraphic(a);
        b.setAlignment(Pos.CENTER);
        b.setPrefSize(300, 100);

        mainLayout.getChildren().addAll(b);

        Scene scene = new Scene(mainLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

This code produces the following results 此代码产生以下结果

When running the program 运行程序时

After resizing window 调整窗口大小后

If the button has some text, if it is for instance created with a single space 如果按钮包含一些文本,例如它是用单个空格创建的

Button b = new Button(" ");

The text does not wrap at all 文字根本没有换行

Partial Solution 部分解决方案

I have not been able to find a solution that allows both centering the text and clipping it if it is too large. 我无法找到一种解决方案,该解决方案既可以将文本居中又可以将其裁剪(如果太大)。 For now, my workaround is to only center the text. 现在,我的解决方法是仅将文本居中。

It is possible to center a TextFlow vertically by setting its maxHeight to something small. 通过将TextFlow的maxHeight设置为较小的值,可以垂直居中。 Using the MCVE for #3 in the question, it is enough to add a.setMaxHeight(0); 使用问题中的#3的MCVE,添加a.setMaxHeight(0);就足够了a.setMaxHeight(0); . This will center the text since the TextFlow will no longer expand to take up all available space. 这将使文本居中,因为TextFlow将不再扩展以占用所有可用空间。

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

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