在Navafx中将节点对齐到Vbox的右侧 [英] Aligning Nodes to the right side of a Vbox in javafx

查看:229
本文介绍了在Navafx中将节点对齐到Vbox的右侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javafx创建一个chatBox,我希望客户端的消息与右边对齐,剩下的消息从左边开始。

I'm trying to make a chatBox with javafx, and I want the messages from the client to be aligned to right and the rest to left.

我'使用 Vbox ,包装在 Scrollpane 中,并在该Vbox中,每条消息都包含在另一个Vbox中。
但是对齐内部Vbox不起作用。

I'm using a Vbox, wrapped in a Scrollpane and in that Vbox, each message is wrapped in another Vbox. But aligning the inner Vbox doesn't work.

private VBox addMsg(String senderName, String text, String time) {
    Label snderName = new Label(senderName + ":");
    snderName.setId("senderName");
    snderName.setMaxWidth(400);
    snderName.setAlignment(Pos.BASELINE_LEFT);

    Label msgText = new Label(text);
    msgText.setId("msgText");
    msgText.setWrapText(true);
    msgText.setMaxWidth(400);

    Label msgTime = new Label(time);
    msgTime.setId("msgTime");
    msgTime.setMaxWidth(400);
    msgTime.setAlignment(Pos.BASELINE_RIGHT);

    VBox msg = new VBox(snderName, msgText, msgTime);
    msg.setBackground(new Background(new BackgroundFill(Color.AQUA, new CornerRadii(3d), Insets.EMPTY)));
    msg.setPadding(new Insets(5));
    msg.setMaxWidth(400);
    msg.setEffect(new DropShadow(2, Color.DARKBLUE));
    return msg;
}

public void buildChatBox() {
    Button backToPublicChat = new Button("<");
    backToPublicChat.setId("backToPublicChat");
    backToPublicChat.setVisible(false);

    Text chatWindowInfo = new Text("public chat room");
    chatWindowInfo.setId("chatWindowInfo");

    VBox chatHistory = new VBox();
    chatHistory.setId("chatHistory");
    chatHistory.setPrefWidth(CHAT_BOX_WIDTH);
    chatHistory.setPrefHeight(GAME_HEIGHT - 50);//badsmell

    TextField messageField = new TextField("type your message...");
    messageField.setId("messageField");
    messageField.setPrefHeight(30);
    messageField.setPrefWidth(CHAT_BOX_WIDTH - 40);

    Button sendButton = new Button("send");
    sendButton.setId("sendButton");
    sendButton.setPrefHeight(30);
    sendButton.setOnMouseClicked(event -> {
        VBox vBox = addMsg(
                "Aran",
                "hi, i'm aran",
                "5:19");
        vBox.setAlignment(Pos.TOP_LEFT);
        chatHistory.getChildren().add(vBox);
        VBox vBox2 = addMsg(
                "Farnam",
                "oh, I love you!",
                "5:19");
        vBox2.setAlignment(Pos.TOP_RIGHT);
        chatHistory.getChildren().add(vBox2);
    });

    ScrollPane scrlPane = new ScrollPane(chatHistory);
    scrlPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
    scrlPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
    scrlPane.setId("scrolPane");
    chatBox = new VBox(new HBox(backToPublicChat, chatWindowInfo), scrlPane, new HBox(messageField, sendButton));
    chatBox.setId("chatBox");

    chatBox.getStylesheets().add("TowDef/GUI//chatBoxCSS.css");
}

另外我只需要设置HPos但我不知道如何,所以我使用了Pos.TOP_RIGHT。
有谁知道如何实现这个目标?
任何有关如何以更好的方式制作聊天框的建议都将受到好评:)

Also I just need to set the HPos but i don't know how to, so I used the Pos.TOP_RIGHT. does anyone know how to achieve this? any advice on how to make the chatbox in a better way will be well appreciated:)

推荐答案

你可以实现使用HBox,

用于右对齐。

You can achieve that using HBox,
For Right Aligning.

Label label=new Label("guru ");
        label.getStylesheets().add("sample/styles/send.css");
        label.setId("receive");
        HBox hBox=new HBox();
        hBox.getChildren().add(label);
        hBox.setAlignment(Pos.BASELINE_RIGHT);
        vBox.getChildren().add(hBox);
        vBox.setSpacing(10);

左对齐

  Label label=new Label(msg);
        label.getStylesheets().add("sample/styles/send.css");
        label.setId("send");
        HBox hBox=new HBox();
        hBox.getChildren().add(label);
        hBox.setAlignment(Pos.BASELINE_LEFT);
        vBox.getChildren().add(hBox);
        vBox.setSpacing(10);

这篇关于在Navafx中将节点对齐到Vbox的右侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆