Get image from relative path

前端 未结 8 2045
囚心锁ツ
囚心锁ツ 2021-02-03 13:01

In my project I have 2 packages. images - contain images and notification - contain java files

In notification/main.java I get Image o

8条回答
  •  情书的邮戳
    2021-02-03 13:13

    Toolkit tk = Toolkit.getDefaultToolkit();
    Class j = YOURJFRAME.getClass();
    Image image = tk.createImage(j.getResource("/images/bell-icon16.png"));
    

    Try that code, if you have a JFrame that will work. If you have an Applet, then just use

    Toolkit tk = Toolkit.getDefaultToolkit();
    Image image = tk.createImage("images/bell-icon16.png");
    

    With applets you never want to use the / in the beginning, but if you have a JFrame, and you are using getResource, you need the / in the beginning of the Path.

提交回复
热议问题