同樣,你確定它的JLabel的錯嗎?當我試圖做一個概念驗證程序時,一切正常 - 看到JPanel的粉紅色背景。例如,
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TransparentJLabel {
private static final String IMAGE_PATH = "http://duke.kenai.com/Oracle/OracleStratSmall.png";
private static void createAndShowUI() {
JPanel panel = new JPanel();
panel.setBackground(Color.pink);
URL imageUrl;
try {
imageUrl = new URL(IMAGE_PATH);
BufferedImage image = ImageIO.read(imageUrl);
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
panel.add(label);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JFrame frame = new JFrame("TransparentJLabel");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
您可能希望自己創建一個類似的計劃,看是否和您的問題,然後張貼在這裏。
確定將JLabel過錯,該圖片是不是透明的,或者在背景不黑?據我瞭解,你的願望應該適用於JLabel中的圖像。 – 2011-01-14 17:50:43
圖片*是*透明的,例如在瀏覽器上正確顯示。 – dm76 2011-01-14 17:55:59
@ Hovercraft:我認爲你的意思是「圖片*是*透明的」。 – 2011-01-14 17:56:30