我看遍了所有的地方,但我仍然停留在如何找到要放入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