Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在StyleConstants.setFontFamily中使用.ttf_Java_Fonts_Jtextpane - Fatal编程技术网

Java 在StyleConstants.setFontFamily中使用.ttf

Java 在StyleConstants.setFontFamily中使用.ttf,java,fonts,jtextpane,Java,Fonts,Jtextpane,所以我试图在一个项目中使用苹果II字体,但我很难弄明白 我可以让它作为字体对象正常工作,但是StyleConstants.setFontFamily使用参数(MutableAttributeSet,String)。我如何将这个.ttf转换成setFontFamily可以使用的东西 public class PageLayout implements ActionListener { .... static Font appleII = new Font("Calibri", Font

所以我试图在一个项目中使用苹果II字体,但我很难弄明白

我可以让它作为字体对象正常工作,但是StyleConstants.setFontFamily使用参数(MutableAttributeSet,String)。我如何将这个.ttf转换成setFontFamily可以使用的东西

public class PageLayout implements ActionListener
{  
  ....
  static Font appleII = new Font("Calibri", Font.BOLD, 40); 
  ....
  public static void createAIIFont()
  {
    try
    {
      FileInputStream fis = new FileInputStream(new File("PrintChar21.ttf"))
      Font base = Font.createFont(Font.TRUETYPE_FONT, fis);
      appleII = base.deriveFont(Font.PLAIN, 24);
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }

  public static void setText(String text)
  {
    SimpleAttributeSet attribs = new SimpleAttributeSet();  
    StyleConstants.setFontFamily(attribs, "//This is where the issue is");
    int len = pane.getDocument().getLength();
    pane.setCaretPosition(len);
    pane.setParagraphAttributes(attribs,true);
    pane.replaceSelection(text);
  }
}

发帖提示:当你添加代码时,用两个空格替换标签,因为它可以让人们阅读更多代码而无需滚动。出于同样的原因,删除多余的换行符。需要滚动的人越少,他们就越容易立即看到你的问题,他们就越不想继续下一个问题而不是花时间滚动你的代码。你试图为什么设置字体族属性?看看方法签名,你做得很对,那么如果你使用合法的字体系列字符串会发生什么呢?(比如
abcdefghij
)我可能只是理解得不好,但如果我将其设置为serif,它会将字体更改为serif。但是,如果我将其设置为appleII或PrintChar21,它将不起作用。字体系列与字体名称不同。这就是说,看看这里的API,这些函数毫无意义,你到底想用这段代码做什么?我从.txt中提取文本,使用setText(我排除了其他行)更改字体大小、颜色和对齐方式,然后用pane.replaceSelection将其显示在JTextPane中。顺便说一句,我感谢你的帮助。