简体   繁体   English

Java图标常量-静态常量可以吗?

[英]Java icon constants - Are static constants ok?

I have a number of icons used throughout an application - let's take ok/cancel icons as an example. 我在整个应用程序中使用了许多图标-让我们以“确定/取消”图标为例。 At the moment they might be a tick and a cross (tick.png, cross.png) but I may want to replace them in future. 目前,它们可能是一个刻度和一个十字(tick.png,cross.png),但我将来可能希望替换它们。 Also, I would like to keep the resource path in one place. 另外,我想将资源路径保持在一个地方。

Is this ok: 这个可以吗:

public class Icons {
    public static Icon OK = new ImageIcon(Icons.class.getResource("/icons/tick.png");
    public static Icon CANCEL = new ImageIcon(Icons.class.getResource("/icons/cross.png");
}

Or should I be doing this a different way? 还是我应该以不同的方式来做? I don't mind relying on the existence of the image files at runtime since they're in the .jar 我不介意在运行时依赖图像文件的存在,因为它们位于.jar中

Solution

I've used Bent's idea for initialisation, and I've made the constants final: 我使用了Bent的想法进行初始化,并且使常数成为最终值:

public final class Icons {
    private static final Logger logger = Logger.getLogger(Icons.class);

    public static final Icon OK = icon("/icons/add.png");
    public static final Icon CANCEL = icon("/icons/cancel.png");

    private static Icon icon(String path) {
        URL resource = Icons.class.getResource(path);
        if(resource==null) {
            logger.error("Resource "+path+" does not exist");
            return new ImageIcon();
        }
        return new ImageIcon(resource);
    }
}

I see two problems with that, both might be acceptable: 我看到两个问题,两个都可以接受:

  1. It will get hard to debug if your icons are not found or can't be loaded for some reasons. 如果找不到图标或由于某些原因无法加载图标,将很难调试。 Code that runs in static initializers can be tricky, because it's easy to "loose" the exception. 在静态初始化程序中运行的代码可能很棘手,因为很容易“释放”异常。
  2. The class will probably never be unloaded and therefore the resource used by the Icons will never bee freed. 该类可能永远不会被卸载,因此Icons所使用的资源将永远不会被释放。

Number 1 is probably acceptable if you can make sure that the icons are always there and can even be worked around by putting the initialization in a static initializer block and adding good exception handling and logging. 如果可以确保图标始终存在并且可以通过将初始化放在静态初始化程序块中并添加良好的异常处理和日志记录来解决,则数字1可能是可以接受的。

Number 2 is probably acceptable since icons are usually used throughout the entire runtime of the application and they wouldn't be freed long before the application quits anyway. 数字2可能是可以接受的,因为图标通常在应用程序的整个运行时使用,并且它们不会在应用程序退出前很长时间就被释放。

So all in all I'd say that's fine. 所以总的来说,这很好。

您可能需要将常量标记为final。

If you want to keep you icons as static constants, I would extract the instantiation of the ImageIcon objects into a static method; 如果要将图标保留为静态常量,则可以将ImageIcon对象的实例化提取为静态方法。

public static final Icon ok = icon("ok.png");


private static Icon icon(String path) {

    URL resource = Icons.class.getResource("/icons/" + path);
    if (resource == null) {
        // Log something...
        return null;
    }
    return new ImageIcon(resource);
}

This way you have control whenever something fail, and you don't have to repeat yourself in the instantiation. 这样,您可以在发生任何故障时拥有控制权,并且不必在实例化中重复自己。

Also, I would make the constants final. 另外,我将常数设为最终值。

A more general approach could be to use reflection to inspect your Icons-class and load resources for each public static Icon field in the class. 一种更通用的方法是使用反射来检查您的Icons类,并为该类中的每个公共静态Icon字段加载资源。 This way, you would only have to declare a new Icon constant, and the corresponding resource would be loaded automatically based on the name of the constant. 这样,您只需要声明一个新的Icon常数,相应的资源将根据该常数的名称自动加载。 Leave a comment if you want more tips for doing this. 如果您需要更多提示,请发表评论。

That seems to be a fairly easy way to do that. 这似乎是一个相当简单的方法。 Although I would name the images with the same name as what they are for ("ok.png", "cancel.png"). 尽管我将使用与图像相同的名称来命名图像(“ ok.png”,“ cancel.png”)。 And make sure that it is clear that removing or renaming the images may cause issues. 并确保清除或重命名图像可能会引起问题。

This seems to be the standard way of doing things, but I have had issues with before. 这似乎是标准的处理方式,但是我以前遇到过问题。

If you are using Eclipse with Maven and store these icons in maven's resource directory, when Eclipse does one of its automatic builds, it won't copy the icon files to your target/classes directory. 如果您将Eclipse与Maven一起使用并将这些图标存储在maven的资源目录中,则当Eclipse执行其自动构建之一时,它将不会将图标文件复制到您的target / classes目录中。 This will cause a runtime exception when it can't find the icons. 当找不到图标时,这将导致运行时异常。

You have to manually do a maven package at least once to get the icons in the right spot. 您必须至少手动执行一次Maven软件包,才能将图标放置在正确的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM