如何在Java中使用汉字?
我正在尝试在 Java GUI 组件中使用中文字符。我已将键盘输出更改为中文,并且可以用中文输入 Netbeans。此外,我还可以编译这些Java文件。但是,当我运行这些程序时,这些字符显示为英文问号。我可以做什么来改变这种情况?
I'm trying to use Chinese characters in Java GUI components. I have changed my keyboard output to Chinese and can type in Netbeans in Chinese. Further, I can compile these Java files. However, when I run these programs, the characters are displayed as English question marks. What can I do to change this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您必须确保使用 javac 编码选项的合适设置进行编译。
其次,您必须使用合适的字符编码来运行。在大多数情况下,设置 -Dfile.encoding=UTF8 就可以做到这一点,但这也取决于程序类型(命令行?GUI?)以及运行它的环境。
First, you have to make sure that you are compiling with a suitable setting for the encoding option to javac.
Second, you have to be running with a suitable character encoding. In most cases, setting -Dfile.encoding=UTF8 will do it, but it also depends on what sort of program (command line? GUI?) and what environment you're running it in.
我不认为最短的答案是最好的,所以我建议您阅读这个优秀的博客Joel Spolsky 发表的文章“每个软件开发人员绝对、绝对必须了解的 Unicode 和字符集(没有任何借口!)”。
我认为花几分钟阅读这篇文章是非常值得的。
I don't think the shortest answer is best here so I recommend you read this excellent blog post on "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)" by Joel Spolsky.
I think the couple minutes spent reading this will be more than worth it.