Swing 不显示 unicode 字符

发布于 2024-10-23 12:19:55 字数 1051 浏览 3 评论 0原文

我尝试在 Swing JComboBox 中显示一些非 ASCII 字符。字符显示不正确,我收到很多奇怪的字符,而非 ascii 字符应该是: Garbled ComboBox

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 中的撇号略有不同,这就是整个事情的开始。)

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(TM) SE 运行时环境(内部版本 1.6.0_22-b04-307-10M3261)

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:
Garbled ComboBox

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

記柔刀 2024-10-30 12:19:55

确保您的编译器使用与编辑器相同的编码(您的编辑器似乎已经使用与控制台相同的编码,并且编译器通常使用 VM 的默认编码,由 file.encoding 给出)财产)。

您可以通过向编译器提供 -encoding 选项或 ant 中的 encoding= 属性来完成此操作。

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.

握住我的手 2024-10-30 12:19:55

您尝试使用的字体没有所需的字形。 canDisplay 方法在 Mac 上由于某种原因失败。在 Linux 和 Windows 上,您的代码按预期运行并且断言失败,但在 Mac 上它不会失败。我在 Mac 上使用某些字符时遇到了类似的问题,我只是使用 Sans 字体,因为它看起来是最完整的 Unicode 字体。如果您查看此线程,您会发现它很常见的问题。因此,也许您想遵循 Costis 在评论中建议的内容,并查看 这个

The font you are trying to use doesn't have the required glyphs. canDisplay methods fail for some reason on Mac. On linux and windows your code behaves as expected and assertion fails, but on Mac it doesn't fail. I had a similar problem with using some characters on Mac, I just went with Sans font cause it seemed like the most Unicode complete font. If you check out this thread you will find out its quite common problem. So maybe you want to go with what Costis suggested in comment and also checking out this.

偏爱你一生 2024-10-30 12:19:55

将 JCombobox 的字体更改为 Unicode 支持的泰米尔语字体。
从下载字体
http://www.ildc.in/Tamil/GIST/htm/otfonts.htm

Font font = new Font("TamilFont", Font.PLAIN, 12);
for (String s : choices) assert font.canDisplayUpTo(s) < 0;
choiceBox.setFont(font);

Changes the font of JCombobox to Unicode supported font of Tamil.
Download font from
http://www.ildc.in/Tamil/GIST/htm/otfonts.htm

Font font = new Font("TamilFont", Font.PLAIN, 12);
for (String s : choices) assert font.canDisplayUpTo(s) < 0;
choiceBox.setFont(font);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文