如何优化使用 Singleton 扩展 JLabel 的 class
How to optimize the class that extends JLabel with Singleton
我有一个扩展 JLabel 的 class。这是 class:
public class LabelFormat extends JLabel {
public LabelFormat(String string){
Font myFont=UtilitySwing.getLabelFont();
this.setText(string);
this.setFont(myFont);
}
}
这是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;
}
所以 class 找到了,但对我来说代码不是很有效,因为如果我创建 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 次 class UtilitySwing 来计算标签的字体。我想是否可以使用单例模式调用一次 UtilitySwing 来计算字体。
为此,也可以在主 class 中创建字体并将其设置为所有标签,但我想创建一个 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
s 使用的字体,假设您希望更改是全局的
这会让您产生创建自定义外观委托的想法,您可以对其提供更好的控制并且只影响 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;
}
如果您不在乎,只是想节省时间而不是重复决策过程一次...
我有一个扩展 JLabel 的 class。这是 class:
public class LabelFormat extends JLabel {
public LabelFormat(String string){
Font myFont=UtilitySwing.getLabelFont();
this.setText(string);
this.setFont(myFont);
}
}
这是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;
}
所以 class 找到了,但对我来说代码不是很有效,因为如果我创建 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 次 class UtilitySwing 来计算标签的字体。我想是否可以使用单例模式调用一次 UtilitySwing 来计算字体。
为此,也可以在主 class 中创建字体并将其设置为所有标签,但我想创建一个 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
s 使用的字体,假设您希望更改是全局的
这会让您产生创建自定义外观委托的想法,您可以对其提供更好的控制并且只影响 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;
}
如果您不在乎,只是想节省时间而不是重复决策过程一次...