如果您只是使用new Font("Arial", Font.PLAIN, 10)
创建字体,稍后,当您尝试以该字体显示缺少的字形时,您将获得指示缺少字形的熟悉方块。
很久以前,我们找到了解决方法 - 将字体传递给FontUtilities.getCompositeFontUIResource(Font)
,然后返回Font
来处理字体本身不属于的字符的后备。
问题是,该实用程序在sun.font
中,我想消除编译器警告。
鉴于在此期间已经过去多年,现在是否有适当的方法来做到这一点?
演示:
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import sun.font.FontUtilities;
public class TestFonts implements Runnable
{
@Override
public void run()
{
Font font = new Font("Arial", Font.PLAIN, 20);
JLabel label1 = new JLabel("Before \u30C6\u30B9\u30C8");
label1.setFont(font);
JLabel label2 = new JLabel("After \u30C6\u30B9\u30C8");
label2.setFont(FontUtilities.getCompositeFontUIResource(font));
JFrame frame = new JFrame("Font Test");
frame.setLayout(new GridLayout(2, 1));
frame.add(label1);
frame.add(label2);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new TestFonts());
}
}
结果:
答案 0 :(得分:0)
您可以利用JComponents能够显示HTML的事实,并通过将这些字符放在<span>
中来覆盖JComponent的字体无法显示的字体的字体:
import java.util.Formatter;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import sun.font.FontUtilities;
public class TestFonts implements Runnable
{
/**
* Replaces plain text meant to be displayed in a JComponent with
* HTML that forces the font to Dialog for any characters which the
* specified font cannot natively display.
*
* @param originalText text to transform to HTML, with forced fonts
* where needed
* @param font default font which will be used to display text
*
* @return HTML version of original text, which forces fonts where
* necessary to ensure all characters will be displayed
*/
static String toCompositeFontText(String originalText,
Font font) {
Formatter html = new Formatter();
html.format("%s", "<html><body style='white-space: nowrap'>");
boolean fontOverride = false;
int len = originalText.length();
for (int i = 0; i < len; i = originalText.offsetByCodePoints(i, 1)) {
int c = originalText.codePointAt(i);
if (font.canDisplay(c)) {
if (fontOverride) {
html.format("%s", "</span>");
fontOverride = false;
}
} else {
if (!fontOverride) {
html.format("<span style='font-family: \"%s\"'>",
Font.DIALOG);
fontOverride = true;
}
}
if (c == '<' || c == '>' || c == '&' || c < 32 || c >= 127) {
html.format("&#%d;", c);
} else {
html.format("%c", c);
}
}
if (fontOverride) {
html.format("%s", "</span>");
}
html.format("%s", "</body></html>");
return html.toString();
}
/**
* Replaces text of the specified JLabel with HTML that contains the
* same text, but forcing the font to Dialog for any characters which
* the JLabel's current font cannot display.
*
* @param label JLabel whose text will be adjusted and replaced
*/
static void adjustText(JLabel label) {
label.setText(toCompositeFontText(label.getText(), label.getFont()));
}
@Override
public void run()
{
Font font = new Font("Arial", Font.PLAIN, 20);
JLabel label1 = new JLabel("Before \u30C6\u30B9\u30C8");
label1.setFont(font);
JLabel label2 = new JLabel("After \u30C6\u30B9\u30C8");
label2.setFont(FontUtilities.getCompositeFontUIResource(font));
JLabel label3 = new JLabel("Corrected \u30C6\u30B9\u30C8");
label3.setFont(font);
adjustText(label3);
JFrame frame = new JFrame("Font Test");
frame.setLayout(new GridLayout(3, 1));
frame.add(label1);
frame.add(label2);
frame.add(label3);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new TestFonts());
}
}
<强>更新强>
此外,您可以监控标签的text
属性,以便自动执行此操作:
label.addPropertyChangeListener("text", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
String text = (String) event.getNewValue();
if (text != null && !text.startsWith("<html>")) {
adjustText((JLabel) event.getSource());
}
}
});
明显的缺点是没有(简单)方法来调整已经以<html>
开头的文本。 (实际上我很确定即使可以通过将标签的文本加载为HTMLDocument来完成。)
答案 1 :(得分:0)
在深入挖掘JDK源代码之后,我发现了一个直接调用内部方法的公共方法。我不知道这是多么狡猾,但它至少是公共API。 :)
label2.setFont(StyleContext.getDefaultStyleContext()
.getFont(familyName, style, size));