How do I add an icon as a classpath resource to an SWT window created with WindowBuilder?

前端 未结 6 598
名媛妹妹
名媛妹妹 2021-02-07 09:48

I\'m trying to add an external icon from an *.ico file to a window that I\'m creating using the WindowBuilder design window. I can select the shell, which brings up an \"image\"

6条回答
  •  别跟我提以往
    2021-02-07 09:59

    I don't know how to do this in WindowBuilder, but you can specify an Image resource while building the Shell via setImage() or setImages(). I suggest using the latter, because it provides the platform with various resolution icons, including the window's control box, the Windows taskbar, and alt+tab list.

    Take a look at this snippet.

    To load it from a resource:

    final Image small = new Image(shell.getDisplay(),
            "resources/images/icon_16.png");
    final Image large = new Image(shell.getDisplay(),
            "resources/images/icon_32.png");
    final Image[] images = new Image[] { small, large };
    shell.setImages(images);
    

    In this example, I have a subfolder "resources", containing "images", then two PNGs. Specifying a resource JAR should work in a similar way, although I haven't tried it.

提交回复
热议问题