I have a class that extend JLabel. This is the class:
public class LabelFormat extends JLabel {
public LabelFormat(String string){
Font myFont=UtilitySwing.getLabelFont();
this.setText(string);
this.setFont(myFont);
}
}
This is the method in the UtilitySwing class:
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;
}
So that class found but for me the code is not very effcient, because if I create 5 label I have this:
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");
Whit this code, I call 5 times the class UtilitySwing to calculate the font of the Label. I thought if is possible to use a singleton pattern to call once the UtilitySwing to calculate the Font.
For this, is possible too create the Font in the main class and set it to all Label but I want to create a jar lib and the user need not worry to set the font.
Call UtilitySwing.getLabelFont()
before you create the labels and set there font properties...
Font font = UtilitySwing.getLabelFont();
LabelFormat label1 = new LabelFormat("Pippo");
label1.setFont(font);
//...
But I'd be trying to put in a factory method or loop to make this easier...
Font font = UtilitySwing.getLabelFont();
LabelFormat label1 = createFormatLabel("Pippo", font);
Use UIManager.put("Label.font", UtilitySwing.getLabelFont());
which set the font to be used by ALL JLabel
s, assuming you want the change to be global
This would lead you onto the idea of maybe creating a custom look and feel delegate which you could provide better control for and only effect the LabelFormat
instances instead
Cache the result...
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;
}
If you think the default screen size might change or...
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;
}
If you don't care and just want to save time and not repeat the decision making process more then once...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.