JTabbedPane中的图标未显示

use*_*477 0 java swing image embedded-resource imageicon

我的标签有问题:

JTabbedPane tab = new JTabbedPane();
    frame.add(tab, BorderLayout.CENTER);

    JPanel contact = new JPanel();
    contact.add(backgroundContact);
    tab.add("Contacto", contact);
    //tab.addTab("Contacto",new ImageIcon("images/image2.gif"), contact,"");

    JPanel schedule = new JPanel();
    schedule.add(backgroundSchedule);
    tab.add("Horario", schedule);
    //tab.addTab("Horario", new ImageIcon("images/image2.gif"), schedule,"");

    JPanel cost = new JPanel();
    cost.add(backgroundCost);
    tab.add("Tarifas", cost);
    //tab.addTab("Tarifas", new ImageIcon("images/image3.gif"), cost,"");


      // Los iconos
    tab.setIconAt(0, new ImageIcon("images/image1.gif"));
    tab.setIconAt(1, new ImageIcon("images/image2.gif"));
    tab.setIconAt(2, new ImageIcon("images/image3.gif"));
Run Code Online (Sandbox Code Playgroud)

我已经尝试了两个选项,但图标没有显示.为什么会这样?

我也试过:new ImageIcon("images/im.gif")哪个不存在,我没有任何错误

And*_*son 6

试试这个:

URL urlToImage3 = this.getClass().getResource("/" + "images/image3.gif");
... new ImageIcon(urlToImage3);
Run Code Online (Sandbox Code Playgroud)

你可能会连接"/" + "images/image3.gif"- 我只想强调领先/,因为从类路径的根搜索更健壮.

如果这些图像是我怀疑的"嵌入资源",它们将File在运行时不可用,但应该位于应用程序的一个Jars中的类路径上,因此可以通过URL.