gpt4 book ai didi

java - Swing:马拉雅拉姆语字符串无法在 JFrame 中正确呈现

转载 作者:行者123 更新时间:2023-11-30 09:16:21 28 4
gpt4 key购买 nike

我试图在 Jframe 中显示马拉雅拉姆语字符串。

JFrame frame = new JFrame("InputDialog");
JOptionPane.showMessageDialog(frame, word );

我正在将相同的字符串写入属性 xml 文件。

PropertyManager.setProperty("Result.xml", "result", word);

字符串在 xml 文件中正确显示: Malayalam in xml file.但它在 JFrame 中显示错误: Malayalam in Jframe如何使 malayalam 字符串在 Swing 中正确呈现?

中南合作:http://pastebin.com/raw.php?i=CPYEhYkf

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Frame;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

import javax.swing.JOptionPane;

public class MalayalamSSCCE {

public static final String malluString = "എണ്‍പത്തേഴുലക്ഷത്തി അറുപത്തയ്യായിരത്തി നാന്നൂറ്റിമുപ്പത്തിരണ്ടു";
//Download font from http://www.findthatfonts.com/search-31416-hTTF/fonts-download-search-engine-thooliuc.ttf.htm
public static void main(String[] args) throws FontFormatException,
IOException {
Frame frame = new Frame("Message frame.");

BufferedInputStream fontInStream = new BufferedInputStream(
new FileInputStream("ThooliUc.TTF"));
Font font = Font.createFont(Font.TRUETYPE_FONT, fontInStream);
JLabel label = new JLabel(malluString);
label.setFont(font.deriveFont(14f));
JOtionPane.showMessageDialog(frame, label);
}
}

完整来源:https://bitbucket.org/vishnuholmes/wordizer-malayalam/src/fe63167a22413644cbae40ad506cc6112eb9f322/src?at=default

最佳答案

诀窍似乎是创建一个 JLabel 并在将其交给 JOptionPane 之前设置标签上的字体。可能还有一些巧妙的 UI 默认值涵盖了它。参见 UIManager Defaults进一步调查。

enter image description here

我找不到可以热链接(对于 SSCCE)的 Font 版本,但请尝试这个变体。

import java.awt.*;
import javax.swing.*;

public class MalayalamSSCCE {

public static final String malluString = "\u0d0e\u0d23\u0d4d\u200d\u0d2a\u0d24\u0d4d\u0d24\u0d47\u0d34\u0d41\u0d32\u0d15??\u0d4d\u0d37\u0d24\u0d4d\u0d24\u0d3f \u0d05\u0d31\u0d41\u0d2a\u0d24\u0d4d\u0d24\u0d2f\u0d4d\u0d2f\u0d3e\u0d2f\u0d3f\u0d30\u0d24\u0d4d\u0d24\u0d3f \u0d28\u0d3e\u0d28\u0d4d\u0d28\u0d42\u0d31\u0d4d\u0d31\u0d3f\u0d2e\u0d41\u0d2a\u0d4d\u0d2a\u0d24\u0d4d\u0d24\u0d3f\u0d30\u0d23\u0d4d\u0d1f\u0d41";

public static Font getFirstFontThatCanDisplay(String text) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNameArray = ge.getAvailableFontFamilyNames();
for (String name : fontNameArray) {
Font f = new Font(name, Font.PLAIN, 20);
if (f.canDisplayUpTo(text)<0) return f;
}
return null;
}

public static void main(String[] args) {
Font f = getFirstFontThatCanDisplay(malluString);
System.out.println(f);

JLabel l = new JLabel(malluString);
l.setFont(f);

JOptionPane.showMessageDialog(null, l);
}
}

它最终使用:

java.awt.Font[family=Arial Unicode MS,name=Arial Unicode MS,style=plain,size=20]

关于java - Swing:马拉雅拉姆语字符串无法在 JFrame 中正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19464610/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com