简体   繁体   English

如何在JTextArea中显示日语字符

[英]How to display japanese characters in JTextArea

There is strange behaviour of JTextArea when displaying japanese characters - I get well-known blank rectangles instead of kanji. 显示日文字符时JTextArea有奇怪的行为 - 我得到了众所周知的空白矩形而不是汉字。 The mostly strange thing is that JTextField displays them perfectly (in both cases I use "Tahoma" font family). 最奇怪的是JTextField完美地显示它们(在两种情况下我使用“Tahoma”字体系列)。 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! ...在我将日语字符串写入problemTextArea之前,它会显示汉字!

PS Sorry for my English. PS抱歉我的英文。

Upd: I am using Windows Upd:我正在使用Windows

The problem is that JTextArea uses a different default font than JTextField. 问题是JTextArea使用与JTextField不同的默认字体。 I had the same problem in an application I wrote that had to support multi-languages. 我在写的一个必须支持多语言的应用程序中遇到了同样的问题。

The reason for your problem is that JTextArea is normally used to show a mono-spaced font, such as Courier New. 您遇到问题的原因是JTextArea通常用于显示单倍间距字体,例如Courier New。 Normally Java contains no additional mappings for a mono-spaced graphical font to display Kanji. 通常,Java不包含用于显示汉字的单行间隔图形字体的附加映射。

The fix you have works, because there is no font named "123", so the default is taken (dialog). 你有的修复工作,因为没有名为“123”的字体,所以默认是采取(对话框)。 The "dialog" font is internally mapped to a font family in the font.properties file of your platform. “对话框”字体在内部映射到平台的font.properties文件中的字体系列。 This will be the same font that JTextField uses. 这将是JTextField使用的相同字体。

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. 您还可以找到JTextArea的特定键并进行更改。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM