java - 如何优化使用 Singleton 扩展 JLabel 的类

标签 java swing singleton jlabel

我有一个扩展 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");

在这段代码中,我调用了 5 次 UtilitySwing 类来计算标签的字体。我想是否可以使用单例模式来调用一次 UtilitySwing 来计算字体。

为此,也可以在主类中创建字体并将其设置为所有标签,但我想创建一个 jar 库,用户不必担心设置字体。

最佳答案

你可以...

在创建标签并设置字体属性之前调用 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 使用的字体,假设你想要全局性的变化

这会让您产生创建自定义外观委托(delegate)的想法,您可以为其提供更好的控制并且只影响 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;
}

如果您不在乎,只是想节省时间而不是重复决策过程那么一次...

关于java - 如何优化使用 Singleton 扩展 JLabel 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29621271/

相关文章:

java - 使用 Java 正则表达式匹配器查找最后一个匹配项

java - 如何在 Wildfly 11 上的异步事件中保留 CDI 上下文?

java - 将图形与关键监听器一起使用

java - 为什么 EvaluatorFilter 不过滤不匹配的语句?

Java - GridBagLayout位置JLabel

design-patterns - 独特资源的单例替代方案

java - JTable 单元格值

java - 设置 JTextField 设置

scala - ActorSystem 单例注入(inject)

Java 最佳实践 : Class with only static methods