【问题标题】:JLabel: issue with accented lettersJLabel:重音字母问题
【发布时间】:2014-05-14 13:18:18
【问题描述】:

我从 JSON Web 服务读取了一些字符串数据。

当我将结果字符串(带有重音字母)放入 JLabel 时,我看到以下结果:

但字符串应包含:Lèttèrè àccèntàtè - àà èè ìì ò

我使用此代码声明JLabel

JLabel descriptionLabel = new JLabel(myString);

如果我尝试将此字符串放入 .txt 文件中,请读取正确的字符串 (Lèttèrè àccèntàtè - àà èè ìì ò)。

这是与我使用的字符集有关的问题吗?

【问题讨论】:

  • 您使用什么代码将文本放入JLabel
  • 字符集和一起/或字体的标准问题
  • @durron597 我使用这个代码:JLabel descriptionLabel = new JLabel(myString);
  • 你在为 JLabel 设置字体吗?
  • descriptionLabel.setFont(new Font(FONT_NAME, Font.BOLD, 12)); with FONT_NAME = "Arial"

标签: java string swing character-encoding jlabel


【解决方案1】:

这对我有用,检查它是否也对你有用,我们可以从那里继续。

public class AccentedLabel extends JFrame {

    public AccentedLabel() {

        JLabel label = new JLabel("áéêè");
        add(label);

        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {

        new AccentedLabel();
    }
}

编辑:现在尝试将字符串中的所有重音字符替换为以下 unicode 字符串并将它们设置在标签中。

á   \u00e0  Á   \u00c0
à   \u00e1  À   \u00c1
â   \u00e2  Â   \u00c2
é   \u00e9  É   \u00c9
è   \u00e8  È   \u00c8
ê   \u00ea  Ê   \u00ca
î   \u00ee  Î   \u00ce
ç   \u00e7  Ç   \u00c7

【讨论】:

  • 感谢您的回复。如果我声明重音字符串“硬编码”,我没有任何问题。我将字符串(从 Web 服务返回)存储在我自己的对象中。
  • @MaTTP 查看我的编辑,告诉我它是否有效,我们将从那里继续。
  • 如果我这样声明 JLabel:descriptionLabel = new JLabel("\u00e8");它有效
  • @MaTTP 我希望您在运行时替换字符串中的字符,例如用String.replace
  • 是的,但这不是最好的方法
猜你喜欢
  • 2014-03-13
  • 2014-04-14
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 2016-05-05
相关资源
最近更新 更多