我有一个扩展JLabel的类。这是班级:
public class LabelFormat extends JLabel {
public LabelFormat(String string){
Font myFont=UtilitySwing.getLabelFont();
this.setText(string);
this.setFont(myFont);
}
}
这是UtilitySwing类中的方法:
public static Font getLabelFont(){
Toolkit t = Toolkit.getDefaultToolkit();
Dimension screenSize = t.getScreenSize();
double width = screenSize.getWidth();
double height= screenSize.getHeight();
Font myFont;
if ((width == 1600.0) && (height == 900.0) ||
(width == 1440.0) && (height == 900.0) ||
(width == 1280) && (height== 800) ||
((width == 1280) && (height== 768)))
{
myFont = new Font("Century Gothic", Font.PLAIN, 14);
}
else if((width==1024) && (height ==600))
{
myFont = new Font("Century Gothic", Font.PLAIN, 12);
}
else if ((width == 1024) && (height== 768))
{
myFont = new Font("Century Gothic", Font.PLAIN, 12);
}
else if ((width == 800) && (height== 600))
{
myFont = new Font("Century Gothic", Font.PLAIN, 11);
}
else{
myFont = new Font("Century Gothic", Font.PLAIN, 11);
}
return myFont;
}
所以这个课程找到了,但对我来说代码不是很有效,因为如果我创建了5个标签我就有这个:
LabelFormat label1 = new LabelFormat("Pippo");
LabelFormat label2 = new LabelFormat("Pippo");
LabelFormat label3 = new LabelFormat("Pippo");
LabelFormat label4 = new LabelFormat("Pippo");
LabelFormat label5 = new LabelFormat("Pippo");
使用此代码,我调用UtilitySwing类5次来计算Label的字体。我想如果可以使用单例模式调用一次UtilitySwing来计算字体。
为此,也可以在主类中创建Font并将其设置为所有Label,但我想创建一个jar lib,用户无需担心设置字体。
答案 0 :(得分:5)
在创建标签之前调用UtilitySwing.getLabelFont()
并设置字体属性...
Font font = UtilitySwing.getLabelFont();
LabelFormat label1 = new LabelFormat("Pippo");
label1.setFont(font);
//...
但我会尝试使用工厂方法或循环来使这更容易......
Font font = UtilitySwing.getLabelFont();
LabelFormat label1 = createFormatLabel("Pippo", font);
使用UIManager.put("Label.font", UtilitySwing.getLabelFont());
设置所有JLabel
使用的字体,假设您希望更改为全局
这会让您想到可能会创建一个自定义外观代理,您可以提供更好的控制权并仅影响LabelFormat
个实例
缓存结果......
private static Map<Dimension, Font> mapFonts = new HashMap<>(25);
public static Font getLabelFont() {
Toolkit t = Toolkit.getDefaultToolkit();
Dimension screenSize = t.getScreenSize();
Font font = mapFonts.get(screenSize);
if (font == null) {
double width = screenSize.getWidth();
double height = screenSize.getHeight();
if ((width == 1600.0) && (height == 900.0)
|| (width == 1440.0) && (height == 900.0)
|| (width == 1280) && (height == 800)
|| ((width == 1280) && (height == 768))) {
font = new Font("Century Gothic", Font.PLAIN, 14);
} else if ((width == 1024) && (height == 600)) {
font = new Font("Century Gothic", Font.PLAIN, 12);
} else if ((width == 1024) && (height == 768)) {
font = new Font("Century Gothic", Font.PLAIN, 12);
} else if ((width == 800) && (height == 600)) {
font = new Font("Century Gothic", Font.PLAIN, 11);
} else {
font = new Font("Century Gothic", Font.PLAIN, 11);
}
if (font != null) {
mapFonts.put(screenSize, font);
}
}
return font;
}
如果您认为默认屏幕尺寸可能会改变或......
private static Font font;
public static Font getLabelFont() {
if (font == null) {
Toolkit t = Toolkit.getDefaultToolkit();
Dimension screenSize = t.getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
if ((width == 1600.0) && (height == 900.0)
|| (width == 1440.0) && (height == 900.0)
|| (width == 1280) && (height == 800)
|| ((width == 1280) && (height == 768))) {
font = new Font("Century Gothic", Font.PLAIN, 14);
} else if ((width == 1024) && (height == 600)) {
font = new Font("Century Gothic", Font.PLAIN, 12);
} else if ((width == 1024) && (height == 768)) {
font = new Font("Century Gothic", Font.PLAIN, 12);
} else if ((width == 800) && (height == 600)) {
font = new Font("Century Gothic", Font.PLAIN, 11);
} else {
font = new Font("Century Gothic", Font.PLAIN, 11);
}
}
return font;
}
如果您不在乎并且只是想节省时间而不是一次又重复决策过程......