Java - 如何在3列中添加JTextPane?

时间:2023-01-27 20:15:16

I'm working on a chat client and I have this problem: What I want to do is divide the JTextPane in 3 colums so that in the first one it puts out the name of the user, in the second the message and the date in the third. I tried to do this by creating 3 separate JTextPane, then synchronized their scrollbars and everything seemed to be working perfectly until I saw how it reacted to the insertion of emotiocons. When the message had multiple lines of code, the JTextPanes for name and date printed new lines but the emoticons have different height and the name and date JTextPanes can't keep up with them. For a single line I solved it by inserting a blank image of dimensions (1px, height of the emoticon), but that works only for one of the lines. Let's get back to my question. I'm not willing to continue brainstorming on how to fix the problem in the case where I have multiple JTextPanes because I'm sure even more problems are going to go out like that. Is there another way to go around it? Can I do it with only one JTextPane and how? Here is a screenshot of how it works with 3 JTextPanes: http://postimage.org/image/611z2807b/ Here is a picture of how I have structured the JTextPanes in Eclipse: http://postimage.org/image/n2ut4bf2f/ I hope that's enough info and thanks in advance! I didn't provide a SSCCE because my code is a few hundred lines and it would really take me a while to just take out the important lines for the question, but if it's trully necessary, I will.

我正在研究聊天客户端,我遇到了这个问题:我想要做的是将JTextPane分成3个列,以便在第一个中输出用户名,在第二个中输出消息和日期第三。我尝试通过创建3个单独的JTextPane来实现这一点,然后同步它们的滚动条,一切似乎都工作得很好,直到我看到它对插入情感的影响。当消息有多行代码时,名称和日期的JTextPanes打印出新行,但表情符号具有不同的高度以及JTextPanes无法跟上它们的名称和日期。对于单行,我通过插入尺寸的空白图像(1px,表情符号的高度)来解决它,但这仅适用于其中一行。让我们回到我的问题。在我有多个JTextPanes的情况下,我不愿意继续头脑风暴如何解决问题,因为我确信更多问题会出现这样的问题。还有另外一种方法吗?我可以只使用一个JTextPane来完成吗?下面是它如何与3个JTextPanes一起工作的截图:http://postimage.org/image/611z2807b/这是我在Eclipse中如何构建JTextPanes的图片:http://postimage.org/image/n2ut4bf2f/我希望这是足够的信息,并提前感谢!我没有提供SSCCE因为我的代码是几百行而且我需要花一些时间才能找出问题的重要部分,但是如果这是必要的,我会的。

3 个解决方案

#1


2  

You can add your custom tables as described here http://java-sl.com/JEditorPaneTables.html

您可以按照http://java-sl.com/JEditorPaneTables.html中的说明添加自定义表格

Add 3 columns table placing in the row's cells your content. You can also set invisible borders if you need.

添加3列表格,在行的单元格中放置您的内容。如果需要,您还可以设置隐形边框。

#2


0  

Some Java Swing components are HTML-capable so there may be a JComponent which allows you to format text output (and maybe input?) the way you're describing.

一些Java Swing组件是支持HTML的,因此可能有一个JComponent允许您按照您描述的方式格式化文本输出(可能还有输入?)。

Many chat clients print the user name and the message in the same field so you can select and copy both the user name and the message.

许多聊天客户端在同一字段中打印用户名和消息,因此您可以选择并复制用户名和消息。

#3


0  

I think you'll be hard pressed to make this work really well with only a single JTextPane, or even a few. You'll end up putting in more work to make it solid than you would taking a simpler approach.

我认为只要使用一个JTextPane,甚至一些JTextPane,你就很难做到这一点。你最终会投入更多的工作来使它比采用更简单的方法更加坚固。

Looking the the pictures you provided, I see a perfect use of a 3 column GridLayout with each column vertically aligned to the top.

查看您提供的图片,我看到完美使用3列GridLayout,每列垂直对齐顶部。

If you find that you encounter performance issues with really long chats, then you can optimize for performance. But don't worry about it until you get there.

如果您发现在长时间聊天时遇到性能问题,那么您可以优化性能。但是在你到达那里之前不要担心。

#1


2  

You can add your custom tables as described here http://java-sl.com/JEditorPaneTables.html

您可以按照http://java-sl.com/JEditorPaneTables.html中的说明添加自定义表格

Add 3 columns table placing in the row's cells your content. You can also set invisible borders if you need.

添加3列表格,在行的单元格中放置您的内容。如果需要,您还可以设置隐形边框。

#2


0  

Some Java Swing components are HTML-capable so there may be a JComponent which allows you to format text output (and maybe input?) the way you're describing.

一些Java Swing组件是支持HTML的,因此可能有一个JComponent允许您按照您描述的方式格式化文本输出(可能还有输入?)。

Many chat clients print the user name and the message in the same field so you can select and copy both the user name and the message.

许多聊天客户端在同一字段中打印用户名和消息,因此您可以选择并复制用户名和消息。

#3


0  

I think you'll be hard pressed to make this work really well with only a single JTextPane, or even a few. You'll end up putting in more work to make it solid than you would taking a simpler approach.

我认为只要使用一个JTextPane,甚至一些JTextPane,你就很难做到这一点。你最终会投入更多的工作来使它比采用更简单的方法更加坚固。

Looking the the pictures you provided, I see a perfect use of a 3 column GridLayout with each column vertically aligned to the top.

查看您提供的图片,我看到完美使用3列GridLayout,每列垂直对齐顶部。

If you find that you encounter performance issues with really long chats, then you can optimize for performance. But don't worry about it until you get there.

如果您发现在长时间聊天时遇到性能问题,那么您可以优化性能。但是在你到达那里之前不要担心。