[英]Problems displaying GUI components with img background
我已經為我的Java Applet添加了一個背景,我需要一些幫助來理解為什么applet沒有正確顯示。 為了顯示這個背景圖像,我使用了下面的代碼:
BufferedImage img = null;
try {
URL url = new URL(getCodeBase(), "Backgrounds/Background.png");
img = ImageIO.read(url);
}
catch (Exception e) {
}
然后還把它放在油漆方法中......
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
問題是,在繪制背景時,您無法看到GUI組件(如按鈕和標簽),即使在將其他GUI組件添加到內容窗格之前繪制了背景。 可以顯示組件,但必須先突出顯示它們或單擊它們。
此圖顯示了加載applet時的applet:
然后這是我在屏幕上的幾個地方點擊后的applet:
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
首先,該方法調用應該是:
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this); // Every Component is an image observer
}
然后,修復破損的油漆鏈:
public void paint(Graphics g) {
super.paint(g); // Draw the rest of the components!
g.drawImage(img, 0, 0, this); // Every Component is an image observer
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.