作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在 Jframe 中显示马拉雅拉姆语字符串。
JFrame frame = new JFrame("InputDialog");
JOptionPane.showMessageDialog(frame, word );
我正在将相同的字符串写入属性 xml 文件。
PropertyManager.setProperty("Result.xml", "result", word);
字符串在 xml 文件中正确显示: 但它在 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);
}
}
最佳答案
诀窍似乎是创建一个 JLabel
并在将其交给 JOptionPane
之前设置标签上的字体。可能还有一些巧妙的 UI 默认值涵盖了它。参见 UIManager Defaults进一步调查。
我找不到可以热链接(对于 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/
我是一名优秀的程序员,十分优秀!