我正在尝试使用javadoc在SimpleAttributeSet上构建一个https://docs.oracle.com/javase/7/docs/api/javax/swing/text/SimpleAttributeSet.html
构造者:
SimpleAttributeSet(AttributeSet source)根据提供的属性集创建新属性集。
因此,我需要构建一个属性集,将其放入构造函数中。查看https://docs.oracle.com/javase/7/docs/api/javax/swing/text/AttributeSet.html中的javadoc,这里没有构造函数。提供的所有方法都返回一些有关属性集的信息,但是没有构造或更改属性集的任何内容。
所以问题是,AttributeSet (然后是SimpleAttributeSet)是如何构造的?
目标是为在StyledDocuments中使用定义几个字体,并将定义字体的所有代码移到一个单独的类中,这样使用它们的代码就更容易读了。
在字体类中:
SimpleAttributeSet myFont = new SimpleAttributeSet(myAttributeSet)在目标班:
doc.insertString(doc.getLength(),"myText",myFont);编辑后添加:
目标就像
public SimpleAttributeSet newFont = new SimpleAttributeSet(
StyleConstants.setFontFamily("SansSerif"),
StyleConstants.setFontSize(16)
);发布于 2017-11-19 01:57:50
目标是为在StyledDocuments中使用定义几个字体,并将定义字体的所有代码移到一个单独的类中,这样使用它们的代码就更容易读了。
也许是这样:
public static class DocumentAttributes
{
private static SimpleAttributeSet font;
private static SimpleAttributeSet boldFont;
public static SimpleAttributeSet getFont()
{
if (font != null)
return font;
font = new SimpleAttributeSet()
StyleConstants.setFontFamily(font, "SansSerif");
StyleConstants.setFontSize(font, 16);
return font;
}
public static SimpleAttributeSet getBoldFont()
{
if (boldFont != null)
return boldFont;
boldfont = new SimpleAttributeSet( getFont() );
StyleConstants.setBold(boldFont, true);
return boldFont;
}
}然后你可以像这样使用它:
doc.insertString(doc.getLength(),"myText", DocumentAttributes.getFont());https://stackoverflow.com/questions/47372655
复制相似问题