[英]JavaFX: How to store and format multiple paragraphs in some kind of text area?
I am currently creating a small chat program (mainly for the purpose of getting some experience with RMI and JavaFX). 我目前正在创建一个小型聊天程序(主要是为了获得RMI和JavaFX的使用经验)。 The chat itself is already finished, now I'm working on the GUI. 聊天本身已经完成,现在我正在使用GUI。 I want every chat message to be printed in some kind of text area similar to this: 我希望每个聊天消息都以类似于以下内容的文本区域打印:
Alice <19:21:35>: Hello World! 爱丽丝<19:21:35>:世界你好!
These are my problems/questions: 这些是我的问题/疑问:
The messages are received as Message objects. 消息将作为消息对象接收。 These objects basically just store username, message, and timestamp; 这些对象基本上只存储用户名,消息和时间戳。 each of them in a seperate attribute. 他们每个人都有一个单独的属性。
The simplest way of doing it would be through a TextArea, although there wouldn't be any neat text formatting, it would just look like a bland NotePad like program. 最简单的方法是通过TextArea,虽然不会有任何整洁的文本格式,但看起来就像是一个乏味的NotePad之类的程序。 I would recommend using a VBox and Texts. 我建议使用VBox和文本。 One VBox could be used to store all the Texts, and you could put the VBox in a ScrollPane for scrolling. 一个VBox可以用来存储所有文本,您可以将VBox放在ScrollPane中进行滚动。 Then, for every message, put an HBox consisting of 3 Texts. 然后,对于每条消息,放置一个由3个文本组成的HBox。 Here is an example 这是一个例子
//Put this method in your Application class
public static void addMessage(Message message){
Text username = new Text(message.getUsername());
username.setFont(Font.font("Verdana", FontWeight.BOLD, 14));
Text date = new Text(message.getTimestamp());
date.setFont(Font.font("Verdana", FontWeight.ITALIC, 12));
date.setFill(Color.GRAY);
Text message = new Text(message.getMessage());
date.setFont(Font.font("Courier New", 12));
mainBox.getChildren().Add(new HBox(username, date, message)); //mainBox being a VBox that stores all your HBoxes.
}
Obviously, change this to suit your needs, and please change the styling. 显然,请更改此设置以适合您的需要,请更改样式。 I just gave an example. 我只是举了一个例子。 This is what I have done before, and with the right styling, it looks a lot nicer than just slopping on a TextArea. 这是我以前做过的,并且使用了正确的样式,它看起来比仅仅倾斜TextArea还要好得多。
If your formatting requirements are fixed, then Text and TextFlow would be a good place to start. 如果您的格式要求是固定的,那么Text和TextFlow将是一个不错的起点。 Each text node can be assigned a style either in code or via CSS. 可以通过代码或CSS为每个文本节点分配样式。 Put multiple text nodes together into a TextFlow. 将多个文本节点放到一个TextFlow中。 Put the TextFlow node into a layout container. 将TextFlow节点放入布局容器中。 Start with a VBox for testing, but I would suggest using a TableView once you get the basics working. 从VBox开始进行测试,但是我建议您在基础知识开始后使用TableView。 The javadoc for TextFlow has snippets of sample code that should get you going. TextFlow的Javadoc中有一些示例代码片段,应该可以帮助您入门。
For a pure JavaFX solution then use multiple Text nodes, styled with CSS. 对于纯JavaFX解决方案,请使用多个CSS样式的Text节点。 Either set formatting properties on each node, or assign a style and use an external .css file. 在每个节点上设置格式设置属性,或者分配样式并使用外部.css文件。 Or if you want to consider third-party components, then something like this: https://github.com/TomasMikula/RichTextFX might be a good place to start. 或者,如果您想考虑第三方组件,则类似以下内容: https : //github.com/TomasMikula/RichTextFX可能是一个不错的起点。
You definitely want to separate your data model from your display model (or view, if you want to use that terminology). 您绝对希望将数据模型与显示模型(或视图,如果要使用该术语)分开。 Build a data class (eg, Message). 建立数据类(例如,消息)。 If you hold your message data in an ObservableList of Message objects then you can bind it to a TableView, and have the table automatically update as you add or remove entries in the list. 如果将消息数据保存在Message对象的ObservableList中,则可以将其绑定到TableView,并在添加或删除列表中的条目时使表自动更新。 Add code to the table cell factories to control how you render each cell from your data model (eg, by creating a TextFlow to draw the content). 将代码添加到表单元格工厂中,以控制如何从数据模型渲染每个单元格(例如,通过创建TextFlow绘制内容)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.