2011-05-25 165 views
2

嘿,所有
我想爲我的JWindow設置背景。我在JWindow中使用了setIconImage方法。但它不起作用設置JWindow背景圖像

怎麼知道問題是什麼?

public MainMenu() throws Exception { 
    try { 
     bg = ImageIO.read(new File("pics" + File.separator 
       + "mainMenuBackground.jpg")); 

     content = new JWindow(this); 
     content.setIconImage(bg); 
     gs.setFullScreenWindow(content); 
     content.repaint(); 

     this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     this.repaint(); 
    } catch (Exception e) { 
     throw new Exception("Some files are unavailable"); 
    } 
} 

這行代碼使一個沒有背景圖像的全屏窗口。爲什麼?
我該如何解決它?

+0

你爲什麼認爲setIconImage會設置窗口的背景圖像?從描述中看,更像是Windows任務欄中顯示的圖標或其他操作系統中的等效項。你可能需要在paintComponent()方法或類似的東西上繪製圖像。 – PhiLho 2011-05-25 12:33:02

+0

[java swing background image]可能的重複(http://stackoverflow.com/questions/2227423/java-swing-background-image) – finnw 2011-05-25 12:48:48

回答

2

setIconImage用於窗口圖標,而不是背景。

試試例如setBackground。如果你想要一些自定義背景圖片,你可能必須重寫一些paint(Graphics g)方法,或者設置一些內容窗格/添加一些繪製圖片的組件。

+0

哎呀!對不起。我只是認爲它必須奏效。我有同樣的想法,但很難做到。它有沒有像setBackGroundImage()? – 2011-05-25 12:36:19