简体   繁体   English

JavaFX:如何在某种文本区域中存储和设置多个段落的格式?

[英]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: 这些是我的问题/疑问:

  1. What JavaFX class to use for this? 为此使用什么JavaFX类? I found a class called TextFlow that seems to be able to do what I want, but I didn't understand how exactly it works by now. 我发现了一个名为TextFlow的类,该类似乎可以执行我想要的操作,但是现在我还不了解它的工作原理。 Or is a simple TextArea enough? 还是简单的TextArea就足够了?
  2. How can I use different formattings within one line? 如何在一行中使用不同的格式? Using HTML? 使用HTML?
  3. How should I keep the received text messages within the client? 如何将收到的短信保存在客户端中? Just always append them to the text area (and have them saved implicitly by the GUI)? 只是总是将它们附加到文本区域(并由GUI隐式保存它们)? Or should I use some kind of ObservableList that buffers the messages and the text area synchronizes with it? 还是应该使用某种ObservableList来缓冲消息,并使文本区域与其同步?

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还要好得多。

  1. 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中有一些示例代码片段,应该可以帮助您入门。

  2. 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可能是一个不错的起点。

  3. 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.

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