我正用Swing写一个简单的记事本,在实现设置显示字体时遇到了麻烦
   我的系统是WinXP,安装了一些额外的字体,比如“华康简魏碑”。   我在程序中,这样写:
   Font myfont = new Font("华康简魏碑",Font.PLAIN, 20);
   textArea.setFont(myfont, Font.PLAIN, 20));
   (其他代码很简单,就不贴上来了,textArea是JTextArea,用来显示打开的文本文件的)   程序运行至setFont时报错,
#
# An unexpected error has been detected by HotSpot Virtual Machine:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x7c931ec3, pid=3428, tid=256
#
# Java VM: Java HotSpot(TM) Client VM (1.5.0-b64 mixed mode, sharing)
# Problematic frame:
# C  [ntdll.dll+0x11ec3]
#
# An error report file with more information is saved as hs_err_pid3428.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#    搜索了一下,似乎是JVM本身的bug,对于一些非标准的字体经常出现这种情况,但不知有什么办法能识别一个Font对象是否会引起这种异常,也就是说:
    Font f = new Font("aName", Font.PLAIN, 20 );
    if(f.check() == true)
    {
        //字体正常,设置字体
    }
    else
    {
    //字体不正常,do nothing
    }
    这个boolean check()应该怎样实现,请教高手,万分谢谢

解决方案 »

  1.   

    关注一下,最近也在考虑这个问题,不过我在setFont得时候不会出异常,之后用的外部字体显示出来的是方框,有文章指出需要在font.properties文件里添加新字体,这样jvm才能识别新字体,不过还没有试验成功!
      

  2.   

    为什么我这里就什么问题都没有,jdk1.5和1.6的都试过了
    只要是文本文件中能够使用的字体都可以使用
    不存在的字体不会抛出异常,系统会使用默认字体
    关注中。
      

  3.   

    看下源码,原来是这样:    public Font(String name, int style, int size) {
            this.name = (name != null) ? name : "Default";
            this.style = (style & ~0x03) == 0 ? style : 0;
            this.size = size;
            this.pointSize = size;
        }如果 name == null 的时候就是 "Default",所以如果系统不存在这个字体是不会抛异常的。
    字体存在而抛异常的情况没有遇到过,有待于研究
    不过可以这样解决嘛:Font f = new Font("aName", Font.PLAIN, 20 );
    try {
        // 字体正常,设置字体
    } catch(Exception ex) {
        // 字体不正常,do nothing
    }