我看遍了所有的地方,但我仍然停留在如何找到要放入JPanel的图像的目录。图像应该在哪里?我点击了我的图片的属性,它显示位置:C:\Users\Joseph\Pictures\Background
,图片的名称是random.jpg
。如何使用imageicon(Java GUI)将图像添加到JPanel?
我想要的图像添加到使用TabbedPane的一个标签。这是我迄今为止所做的,我无法做到。
JPanel flPanel = new JPanel();
flPanel.setLayout(new FlowLayout());
ImageIcon image = new ImageIcon(getClass().getResource(""));
// Tried /Users/Joseph/Pictures/Background/random.jpg and doesn't work
JLabel j1 = new JLabel(image);
flPanel.add(j1);
tabbedPane.add("Tab 2", flPanel);
图片是否应该与项目在同一个包文件中?还是应该在源文件中才能够做到“random.jpg”?
从秋千教程阅读部分[如何使用图标](http://docs.oracle.com/javase/tutorial /uiswing/components/icon.html)作为一个工作示例。 – camickr 2014-09-24 03:10:54
请一定回答后,不要破坏您自己的问题。 – 2014-09-24 07:12:00
@TheodorosChatzigiannakis:FYI,他试图用[从今天这个问题(http://stackoverflow.com/questions/26210236/why-is-my-fillpolygon-shape-looking-odd-java-gui再搞破坏问题)。我已通知网站管理员。我们将不得不看他。 – 2014-10-06 22:35:51