Layout – JavaFX – vertically center text in textflow

I am currently using JavaFX's text and textflow layouts. I need to figure out how to place the text node in the center of textflow

The problem is that they are arranged differently When the emoticon is centered, the text remains at the bottom

The green border line represents the border of textflow, and the blue border line represents the border of text

I've tried to set the textorigin property of text to center, but it won't change anything in my case Setting textalignment to center also has no effect

This is an excerpt from my code:

public CChatMessage(String senderName,String messageText)
{
    this.sender = new Label(senderName);
    this.sender.setTextAlignment(TextAlignment.CENTER);
    this.sender.setFont(Font.font("Verdana",FontWeight.EXTRA_BOLD,14));

    this.message = new Text(messageText);
    this.message.setTextAlignment(TextAlignment.CENTER);
    this.message.setTextOrigin(VPos.CENTER);

    this.setEffect(new DropShadow());
    this.setAlignment(Pos.CENTER);
    this.setPadding(new Insets(0,10,10));

    TextFlow messagePane = new TextFlow();
    messagePane.setStyle("-fx-border-color: green");
    messagePane.setTextAlignment(TextAlignment.CENTER);
    Image smileyImage = new Image("/resources/smiley.png");

    messagePane.getChildren().addAll(this.message,new ImageView(smileyImage),new ImageView(smileyImage));

    if(!senderName.equals(""))
    {
        CChatMessage.setMargin(messagePane,new Insets(10,0));
        this.message.setFont(Font.font("Calibri",FontWeight.SEMI_BOLD,18));
        this.getChildren().addAll(this.sender,messagePane);
    }
    else
    {
        this.setPadding(new Insets(5,5,5));
        message.setFont(Font.font("Verdana",11));
        this.getChildren().add(messagePane);
    }
}

Solution

Editor: I think this is the answer you are looking for: https://bugs.openjdk.java.net/browse/JDK-8098128

Editor 2: it seems that the solution I give below has problems Words in the text node are not retained in the H@R_870_2419 @Yes So far, I haven't figured out how to solve this problem But when the text node is in the textflow container, it does remain inside

The problem has been solved there I don't fully understand it, but I hope you do

I will leave the original answer because it contains the way I deal with the problem

This solution may work After not centering the text node like this, I have solved this problem: I use a H@R_870_2419 @Instead of textflow It finished the work for me The behavior is similar enough that I can align the text node in the way I want

But please note that I'm just a novice Therefore, if you use this method, problems may occur I don't know textflow and H@R_870_2419 @The attribute of is enough to answer confidently But I just think I'll tell you my solution, because that's what I'm using for my project now (Editor: as you read in Editor 2 above, I have a problem. There may be more problems.):)

The code of happiness

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>