2010-07-16 68 views
7

我試圖在JTextArea中打印unicode。我已經將它打印到控制檯上,但是當我嘗試將它打印到textarea時,我得到了所有兩個字節的unicode字符的框。任何幫助將非常感激。在Java JTextArea中顯示Unicode

package edu.afit.jieddo; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class JTextAreaDemo extends JFrame { 
    StringBuffer m = new StringBuffer("\u14c7 \u14c4 \u1557 \u00d6"); 
    StringBuffer m2 =new StringBuffer(" means one."); 
    String message = m.append(m2).toString(); 

    public JTextAreaDemo() { 
     super("\u14c7 \u14c4 \u1557 \u00d6"); 
     java.awt.Font font = new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 18); 
     JTextArea textArea = new JTextArea(message); 
     textArea.setFont(font); 

     java.awt.Container container=this.getContentPane(); 
     container.add(textArea); 
     System.out.println(textArea.getFont().getFamily());// testing output in the command line 
    } 

    public static void main(String[] args) { 
     JTextAreaDemo frame = new JTextAreaDemo(); 
     frame.setFont(new Font("Arial Unicode MS",Font.ITALIC,11)); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    System.out.println("\u14c7 \u14c4 \u1557 \u00d6"); 
     System.out.println(frame.getFont().getFamily());//testing output in the command line 
    } 
} 

回答

3

在這些Unicode字符中,Arial Unicode MS僅提供U + 00d6。

嘗試使用DejaVu Sans

順便說一句,FileFormat.info是一個很好的Unicode字符資源。只需將此URL中的XXXX替換爲unicode編號: http://www.fileformat.info/info/unicode/char/XXXX/index.htm。例如:http://www.fileformat.info/info/unicode/char/14C7/index.htm

+0

德文郡,完美!雖然我沒有DejaVu Sans,但我確實有Euphemia,並立即修復它。非常感謝! – JimmyButterfly 2010-07-16 16:43:13

1

在我的Ubuntu系統上,它可能從來沒有聽說過「Arial Unicode MS」,你的程序執行不變,完美無瑕。我在標題欄和文本區域看到的前兩個字符看起來像蝸牛,指向不同的方向。或像小寫字母db躺在背上。

除了尺寸的變化,我看到相同的字符,當我刪除setFont調用。

因此,這是我的教育猜測,你正在使用的字體不包含這些字符。呃,這些角色的字形。

在Windows中有一個字體查看器工具,您可以查看字體中的所有字符。蝸牛出現了嗎?

+0

卡爾,非常感謝讓我知道我並不瘋狂。我花了一週的時間查看看起來不錯的代碼,並在留言板上留下未答覆的消息。我很高興我找到了stackoverflow! 週末愉快! – JimmyButterfly 2010-07-16 16:44:24