JTabbedPane中的图标未显示 [英] icon in JTabbedPane is not shown
问题描述
我的标签出现问题:
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"));
我已尝试过两种选项,但图标未显示。为什么会这样?
I've tried both options, but the icons are not shown. Why is it happening?
我也试过:新的ImageIcon(images / im.gif)
哪个不存在而我没有任何错误
I also tried: new ImageIcon("images/im.gif")
which doesn't exist and I haven any error
推荐答案
试试这个:
URL urlToImage3 = this.getClass().getResource("/" + "images/image3.gif");
... new ImageIcon(urlToImage3);
您可以连接/+images / image3.gif
- 我只想突出显示领先的 /
,因为从类路径的根搜索更加健壮。
You might concatenate "/" + "images/image3.gif"
- I just wanted to highlight the leading /
, since it is more robust to search from the root of the class-path.
如果我怀疑这些图像是嵌入式资源,那么在运行时它们将无法通过文件
获得,但是在应用程序的一个Jars中的类路径上,因此可以通过 URL
获得。
If these images are an 'embedded resource' as I suspect, they will not be available by File
at run-time, but should be on the class-path in one of the Jars of the app., and therefore available by URL
.
这篇关于JTabbedPane中的图标未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!