bir*_*tri 1 java singleton swing jlabel
我有一个扩展JLabel的类.这是班级:
public class LabelFormat extends JLabel {
public LabelFormat(String string){
Font myFont=UtilitySwing.getLabelFont();
this.setText(string);
this.setFont(myFont);
}
}
Run Code Online (Sandbox Code Playgroud)
这是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;
}
Run Code Online (Sandbox Code Playgroud)
所以这个类找到了,但对我来说代码不是很有效,因为如果我创建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");
Run Code Online (Sandbox Code Playgroud)
使用此代码,我调用UtilitySwing类5次来计算Label的字体.我想如果可以使用单例模式调用一次UtilitySwing来计算字体.
为此,也可以在主类中创建Font并将其设置为所有Label,但我想创建一个jar lib,用户不必担心设置字体.
UtilitySwing.getLabelFont()在创建标签之前调用并设置字体属性...
Font font = UtilitySwing.getLabelFont();
LabelFormat label1 = new LabelFormat("Pippo");
label1.setFont(font);
//...
Run Code Online (Sandbox Code Playgroud)
但我会尝试使用工厂方法或循环来使这更容易......
Font font = UtilitySwing.getLabelFont();
LabelFormat label1 = createFormatLabel("Pippo", font);
Run Code Online (Sandbox Code Playgroud)
使用UIManager.put("Label.font", UtilitySwing.getLabelFont());设置ALL所使用的字体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;
}
Run Code Online (Sandbox Code Playgroud)
如果您认为默认屏幕尺寸可能会更改或...
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;
}
Run Code Online (Sandbox Code Playgroud)
如果你不在乎,只是想节省时间,不再重复决策过程一次......