关于构建聊天框的 Java GUI 指南

发布于 2024-08-24 16:47:29 字数 98 浏览 5 评论 0原文

我只是在寻找有关如何实现聊天框的一些指导。

需要有一个用于用户输入的文本区域,以及过去消息的滚动列表。有没有一种简单/标准的方法可以做到这一点?

谢谢

I'm just looking for a little guidance as to how i might implement a chat box.

There needs to be a text area for user entry, and a scrolling list of past messages. Is there an easy/standard way of going about doing this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里梦着梦中梦 2024-08-31 16:47:29

我不确定对以前的消息使用 JList 的建议..这似乎不是我在聊天客户端中寻找的行为。

我建议您查看 JTextPane 类,该类适用于更多“文档”类型数据,例如正在运行的聊天对话。将数据动态附加到文档中非常容易,您甚至可以将样式应用于文档的某些部分(例如,您想以粗体和不同的颜色显示各个聊天参与者的用户名)。

以下是一些示例链接:

如何使用编辑器窗格和文本窗格

更多 TextPane 具体示例

您可能需要考虑的另一件事是线程,例如您可能希望有一个后台线程来轮询新消息,以便在您寻找要显示的新消息时 UI 不会挂起。

I'm not sure about the suggestion for using a JList for previous messages .. that doesn't seem like the behavior I'd be looking for in a chat client.

I would recommend taking a look at the JTextPane class, which is intended for more "document" type data such as a running chat conversation. It is very easy to append data on the fly to the document and you can even apply styling to portions of the document (say for instance you want to show the user names of the various chat participants in bold and in different colors).

Here are a couple of links to examples:

How to Use Editor Panes and Text Panes

More TextPane specific examples

The other thing you'll probably want to think about is threading, for instance you would likely want to have a background thread that is polling for new messages so that the UI doesn't hang while you're looking for new messages to display.

心头的小情儿 2024-08-31 16:47:29

好吧,我想你已经做到了这一切。

您使用 JTextArea 让用户输入文本,然后使用 JList 来输入上一条消息,并使用从服务器上轮询的消息更新的 ListModel。

Well, I think you made it all.

You use a JTextArea to let users enter text, then a JList for previous message, with a ListModel you update from messages polled on server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文