如何在Swing应用程序中实现UTF-8格式? [英] how to implement UTF-8 format in Swing application?

查看:372
本文介绍了如何在Swing应用程序中实现UTF-8格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Swing聊天应用程序中,我有发送按钮,一个文本区域和一个文本字段。



如果我按发送按钮,我需要将文本从文本字段发送到文本区域。



请提供一些想法或代码,帮助我解决这个问题。

解决方案

首先,String的内部字符表示形式是UTF-16,因此,一旦在JVM中有字符串,就不需要担心。 / p>

问题可能是通过互联网发送的字符序列和String对象之间的转换。当解析字符串时,您需要提供编码,例如。当使用 InputStreamReader 时,必须传递 Charset 参数:


InputStreamReader(InputStream in,Charset cs)

创建一个使用给定字符集的InputStreamReader。


必须提供编码,因为Java不能神奇地猜出字节序列的编码。


In my Swing chat application i am having the send button, one text area, and a text field.

If I press Send button, I need to send the text from text field to text area. It is working fine in English but not in local language.

Please give some idea or some code that will help me to solve this.

解决方案

First of all, the internal character representation of String is UTF-16, so you don't need to worry once you have the string in your JVM.

The problem is probably the conversion between a sequence of characters that gets sent over the internet and a String object. When parsing a string you need to provide the encoding, e.g. when using InputStreamReader, you have to pass the Charset parameter:

InputStreamReader(InputStream in, Charset cs)
Create an InputStreamReader that uses the given charset.

The encoding has to be provided, because Java can't magically guess the encoding of a byte sequence.

这篇关于如何在Swing应用程序中实现UTF-8格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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