如何在JTextArea中显示日语字符 [英] How to display japanese characters in JTextArea

查看:38
本文介绍了如何在JTextArea中显示日语字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显示日语字符时,JTextArea的行为很奇怪-我得到了众所周知的空白矩形而不是日文汉字.最奇怪的是,JTextField完美地显示了它们(在两种情况下,我都使用"Tahoma"字体家族).另外,如果我输入以下代码:

There is strange behaviour of JTextArea when displaying japanese characters - I get well-known blank rectangles instead of kanji. The mostly strange thing is that JTextField displays them perfectly (in both cases I use "Tahoma" font family). Also, if I put this code:

    Font f = new Font("123", Font.PLAIN, 12); // This font doesn't exists
    problemTextArea.setFont(f);

...在将日语字符串写入问题文本区域之前,它显示汉字!

...before i write japanese string to the problemTextArea it displays kanji!

P.S.对不起,我的英语.

P.S. Sorry for my English.

更新:我正在使用Windows

Upd: I am using Windows

推荐答案

问题是JTextArea使用的默认字体与JTextField不同.在我编写的必须支持多语言的应用程序中,我遇到了同样的问题.

The problem is that JTextArea uses a different default font than JTextField. I had the same problem in an application I wrote that had to support multi-languages.

出现问题的原因是JTextArea通常用于显示等宽字体,例如Courier New.通常,Java不包含用于显示汉字的等间距图形字体的其他映射.

The reason for your problem is that JTextArea is normally used to show a mono-spaced font, such as Courier New. Normally Java contains no additional mappings for a mono-spaced graphical font to display Kanji.

您拥有的修复程序有效,因为没有名为"123"的字体,因此采用默认字体(对话框).对话框"字体在内部映射到平台的font.properties文件中的字体系列.这将与JTextField使用的字体相同.

The fix you have works, because there is no font named "123", so the default is taken (dialog). The "dialog" font is internally mapped to a font family in the font.properties file of your platform. This will be the same font that JTextField uses.

我具有以下修复程序,以确保在所有图形组件中使用相同的字体定义.您还可以找到JTextArea的特定键并进行更改.这样,您不必担心任何组件的字体,它们都将通过对话框初始化.

I have the following fix, to ensure that the same font definition is used in ALL graphical components. You can also find the specific key for JTextArea and change it. This way you don't have to worry about the fonts of any component, they will be initialized with dialog.

Object fontDefinition = new UIDefaults.ProxyLazyValue("javax.swing.plaf.FontUIResource", null, new Object[] { "dialog", new Integer(Font.PLAIN), new Integer(12) });

java.util.Enumeration keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
    Object key = keys.nextElement();
    Object value = UIManager.get(key);
    if (value instanceof javax.swing.plaf.FontUIResource) {
        UIManager.put(key, fontDefinition);
    }
}

这篇关于如何在JTextArea中显示日语字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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