Swing不显示unicode字符 [英] Swing not displaying unicode characters
问题描述
我有一些非ASCII字符,我试图在Swing JComboBox中显示。字符显示不正确,我得到很多奇怪的字符,非ASCII字符应该是:
import javax.swing。*;
public class Test {
public static void main(String [] args){
String [] choices = new String [] {Good's,Bad's,தமிழ்};
for(String s:choices)System.out.println(s);
JComboBox choiceBox = new JComboBox(choices);
JFrame frame = new JFrame(Test);
frame.setSize(400,400);
frame.add(choiceBox);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
$(注意在Bad's中略有不同的撇号, )
$ b System.out.println
调用在我的终端显示字符很好。
关于这个,有很多关于这个问题的问题,他们建议从GraphicsEnvironment中列出字体,并且只挑选显示我的字符的字体。
Font font = new Font(Ariel,Font.PLAIN,12 );
for(String s:choices)assert font.canDisplayUpTo(s)< 0;
choiceBox.setFont(font);
断言不会失败,但仍然显示乱码。
我在OSX 10.6.5,Java™SE运行时环境(build 1.6.0_22-b04-307-10M3261)
解决方案确保你的编译器使用与你的编辑器相同的编码(你的编辑器已经和控制台一样使用了,看来,编译器通常使用默认编码的VM file.encoding
property。
您可以通过给 -encoding
选项加入编译器,或者在ant中加入 encoding =
属性。
I've got some non-ascii characters I'm trying to display in a Swing JComboBox. The characters aren't displaying correctly, I get lots of weird characters where the non-ascii characters should be:
import javax.swing.*;
public class Test {
public static void main(String[] args) {
String[] choices = new String[]{"Good's","Bad’s","தமிழ்"};
for (String s : choices) System.out.println(s);
JComboBox choiceBox = new JComboBox(choices);
JFrame frame = new JFrame("Test");
frame.setSize(400, 400);
frame.add(choiceBox);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
(Note the slightly different apostrophe in Bad’s, which is what started this whole thing.)
The System.out.println
call displays the characters just fine in my terminal.
There are a bunch of questions on SO about this, and they suggest listing fonts from the GraphicsEnvironment and picking only ones that claim to display my characters. Unfortunately, this trick doesn't work for me.
Font font = new Font("Ariel", Font.PLAIN, 12);
for (String s : choices) assert font.canDisplayUpTo(s) < 0;
choiceBox.setFont(font);
The assert doesn't fail, but still displays garbled characters.
I'm on OSX 10.6.5, Java(TM) SE Runtime Environment (build 1.6.0_22-b04-307-10M3261)
解决方案 Make sure your compiler uses the same encoding as your editor (your editor already uses the same as the console, it seems, and the compiler normally uses the default encoding of the VM, given by the file.encoding
property).
You can do this by giving the -encoding
option to the compiler, or the encoding=
attribute in ant.
这篇关于Swing不显示unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!