我试图加载自定义字体,它的工作原理。现在我遇到了问题,Iam无法在应用程序中使用多个自定义字体。如果我添加三个不同字体的labelField,则只有最后一个自定义字体用于所有标签。加载多个自定义字体黑莓
如果我删除最后一个标签的setFont,则使用第二个字体。有没有使用最后一种字体的机制?
这里是一个添加LabelFields代码,并设置字体
LabelField TestLabel1 = new LabelField("Test Label 1");
Font fo1 = loadCustomFonts("FirstFont.ttf", "FirstFont", 0, 30);
TestLabel1.setFont(fo1);
add(TestLabel1);
LabelField TestLabel2 = new LabelField("Test Label 2");
Font fo2 = loadCustomFonts("SecondFont.ttf", "SecondFont", 0, 30);
TestLabel2.setFont(fo2);
add(TestLabel2);
LabelField TestLabel3 = new LabelField("Test Label 3");
Font fo3 =loadCustomFonts("ThirdFont.TTF", "ThirdFont", 0, 30);
TestLabel3.setFont(fo3);
add(TestLabel3);
这里是我的“loadCustomFonts”方法,它加载自定义字体,并返回一个字体对象。
public Font loadCustomFonts (String path, String fontname,int fontStyle, int fontSize){
InputStream stream = this.getClass().getResourceAsStream(path);
if (FontManager.getInstance().load(stream, fontname, FontManager.APPLICATION_FONT) == FontManager.SUCCESS){
try{
FontFamily family;
add(new LabelField("A"));
family = FontFamily.forName(fontname);
Font myFont = family.getFont(fontStyle,fontSize);
return myFont;
}
catch (ClassNotFoundException e){System.out.println(e.getMessage());}
}
else {
try{
FontFamily family;
add(new LabelField("B"));
family = FontFamily.forName(fontname);
Font myFont = family.getFont(fontStyle,fontSize);
return myFont;
}
catch (ClassNotFoundException e){System.out.println(e.getMessage());}
}
return null;
}
我试着用下面的覆盖,但它不工作 “的LabelField testLabel2 =新的LabelField(“测试标签2 “){ \t公共无效涂料(图形图像){ \t \t \t字体FO2 = loadCustomFonts(” SmartCourierEUR-ExtraBoldCn.ttf”, “SmartCourierEUR-ExtraBoldCn”,0,30); \t \t \t System.out.println(“font 2:”+ fo2); \t \t \t this.setFont(fo2); \t} }; add(testLabel2);' – Martin
add super.paint(graphics);在setfont –