我的問題是,當向JPanel添加一個.GIF時,它會爲.GIF顯示這個黑色背景。在JPanel上添加一個.GIF的黑色方塊顯示
結果上的JPanel添加時:
它發生時,我用這條線:
p2.add(loadBar); // where p2 = new JPanel();
然而,當我添加在JFrame的同一.GIF時,黑色方塊不再存在了。就像這樣:
jf.add(loadBar); // where jf = new JFrame();
結果在JFrame中添加時:
的類代碼部分:
String loadLink = "http://i.imgur.com/mHm6LYH.gif";
URL ajaxLoad = null;
try {
ajaxLoad = new URL(loadLink);
} catch (MalformedURLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
ImageIcon loading = new ImageIcon(ajaxLoad);
JLabel loadBar = new JLabel(loading);
loadBar.setBounds(70, 60, 54, 55);
loadBar.setOpaque(false);
//jf.add(loadBar);
p2.add(loadBar);
可能有人請解釋爲什麼它正在發生喜歡這個?感謝您的時間和閱讀。
編輯:
// Creates the Initialization Panel
p2 = new JPanel();
// Sets the background, black with 125 as alpha value
// This is less transparent
p2.setLayout(null);
p2.setBackground(new Color(0,0,0,150));
// Sets a border to the JPanel
p2.setBorder(new LineBorder(Color.WHITE));
// Sets some size to the panels
p2.setBounds(20, 20, 200, 150);
// Adds the loading gif
String loadLink = "http://i.imgur.com/mHm6LYH.gif";
URL ajaxLoad = null;
try {
ajaxLoad = new URL(loadLink);
} catch (MalformedURLException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
ImageIcon loading = new ImageIcon(ajaxLoad);
JLabel loadBar = new JLabel(loading);
loadBar.setBounds(70, 60, 54, 55);
loadBar.setOpaque(false);
p2.add(loadBar);
即其無需JLabel中的第一圖像中示出的的JPanel。我不能在代碼中顯示JFrame部分,因爲它遍佈整個班級。但我不認爲問題出在JFrame上,所以可能是這個JPanel:/
它的工作完美適合我,根據您的代碼。你可以試試這個代碼,你沒有任何背景圖像共享。 – Braj
對我來說也很類似的代碼:[GifTest](http://luniks.net/other/GifTest.png) –
請問你們可以試一下我在原帖中發佈的編輯代碼嗎?謝謝! – user3508709