0

我的标签有问题:

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"));

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

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

4

1 回答 1

6

试试这个:

URL urlToImage3 = this.getClass().getResource("/" + "images/image3.gif");
... new ImageIcon(urlToImage3);

您可能会连接"/" + "images/image3.gif"- 我只是想突出显示前导/,因为从类路径的根目录搜索更加健壮。

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

于 2012-05-04T14:27:04.970 回答