java swing 字体显示,如何在Java Swing中使用Wingdings字体

When I try to use the Wingdings font (or other symbol fonts), the text comes out as rectangles instead of the correct text. How do I get the correct characters to show?

import java.awt.*;

import javax.swing.*;

public class WingdingsFontDisplay extends JFrame

{

public static void main(String[] args)

{

new WingdingsFontDisplay();

}

public WingdingsFontDisplay()

{

this.setSize(500,150);

this.setTitle("Fun with Fonts");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//This shows that I do have "Wingdings" available

GraphicsEnvironment g;

g = GraphicsEnvironment.getLocalGraphicsEnvironment();

String[] fonts = g.getAvailableFontFamilyNames();

for(String f : fonts)

{

System.out.println(f);

}

//Displaying text in the Wingdings font shows rectangles

JLabel wingdingsText = new JLabel("All work and no play makes Jack a dull boy");

Font f1 = new Font("Wingdings", Font.PLAIN, 14);

wingdingsText.setFont(f1);

this.add(wingdingsText, BorderLayout.NORTH);

//Displaying text in Arial works correctly

JLabel arialText = new JLabel("All work and no play makes Jack a dull boy");

Font f2 = new Font("Arial", Font.PLAIN, 14);

arialText.setFont(f2);

this.add(arialText, BorderLayout.SOUTH);

this.setVisible(true);

}

}

解决方案

You need to use the appropriate Unicode range for the symbols you seek. In Java, symbols are not overlaid on the ASCII range, but have their own distinct character codes.

You can find a reference to the appropriate symbol codes at http://unicode.org/~asmus/web-wing-ding-ext.pdf . Most common symbols are in the 0x2200 and 0x2700 Unicode ranges.

Your Java install may include the SymbolTest sample applet, which makes it straightforward to preview the presentation of Unicode ranges with available fonts. Be warned, however, that better Java implementations will use font substitutions for symbols or characters not in the specified font, so you'll want to be sure you're actually getting the specified font.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值