在我的界面我有一個空佈局作爲背景標籤持有人,沒有其他組件的JPanel。我試圖在GridLayout的背景上添加一行圖片。我已經確定圖標在添加到JLabel時能夠正常工作,但是當我嘗試將Grid Panel添加到背景面板時,它不顯示圖像。這是到目前爲止我的代碼:JPanel將不會顯示在另一個JPanel
jPanel1 = new javax.swing.JPanel();
backgroundLabel = new javax.swing.JLabel();
backgroundLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Ile/Background.jpg")));
jPanel1.add(backgroundLabel);
,這是背景,然後我嘗試在此之上添加圖像的網格:
JPanel gridPanel = new JPanel(new GridLayout(9,3));
JLabel ashes = new JLabel();
ImageIcon ashesIcon = new ImageIcon(getClass().getClassLoader().getResource("Resources/Ashes.png"));
ashes.setIcon(ashesIcon);
gridPanel.add(ashes);
jPanel1.add(gridPanel);
jPanel2.setVisible(true);
但正在顯示什麼都沒有,有什麼建議?
不要使用空佈局,使用適當的佈局 – Blasanka
Java的圖形用戶界面有不同的OS」,屏幕大小,屏幕分辨率等方面的工作在不同的地區使用不同的PLAFs。因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 –
.. 2)爲了更好地提供幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 3)例如獲取圖像的一種方法是熱鏈接到[本問答](http://stackoverflow.com/q/19209650/418556)中看到的圖像。 –