2012-02-27 81 views
0

每當我添加一個背景(圖像img)到我的JFrame我無法看到我的菜單欄....任何幫助將不勝感激...我只是學習JFrames和可能忽略一些愚蠢的東西。JFrame背景覆蓋菜單欄

class GameFrame extends JFrame { 

private JLabel statusbar; 
Image img = new ImageIcon("splash.png").getImage(); 

public GameFrame() { 
    initUI(); 
    menuUI(); 
    BackgroundLoader bg = new BackgroundLoader(); 
} 

@Override 
    public void paint(Graphics g) { 
    try { 
     Image img = ImageIO.read(new File("splash.png"));    
     g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

public final void initUI() { 
    setTitle("Super RPG Hero: The Quest for Fame and Fortune"); 
    setSize(800, 480); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 

    //JLabel background = new JLabel(splash); 
    //background.setBounds(0, 0, splash.getIconWidth(), splash.getIconHeight()); 
    //getLayeredPane().add(background, new Integer(Integer.MIN_VALUE)); 

} 

public final void menuUI() { 


    JMenuBar menubar = new JMenuBar(); 

    //Creates file menu item 
    JMenu file = new JMenu("File"); 
    file.setMnemonic(KeyEvent.VK_F); 

    //Creates Object for New Game toolbar 
    JMenuItem newItem = new JMenuItem("New Game"); 
    newItem.setMnemonic(KeyEvent.VK_C); 
    newItem.setToolTipText("New Game"); 
    newItem.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent event) { 
      String playerName = "Peter"; 
      CharacterCreator characterOne = new CharacterCreator(playerName); 
      characterOne.statBuilder(); 
     } 
    }); 

    //Creates Object for Save Game toolbar 
    JMenuItem saveItem = new JMenuItem("Save"); 
    saveItem.setMnemonic(KeyEvent.VK_C); 
    saveItem.setToolTipText("Save Game"); 

    //Creates Object for Load Game toolbar 
    JMenuItem loadItem = new JMenuItem("Load"); 
    loadItem.setMnemonic(KeyEvent.VK_C); 
    loadItem.setToolTipText("Load Game"); 

    //Creates Object for Exit Game toolbar 
    //And creates method for the game to exit 
    JMenuItem exitItem = new JMenuItem("Exit"); 
    exitItem.setMnemonic(KeyEvent.VK_C); 
    exitItem.setToolTipText("Exit Game"); 
    exitItem.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent event) { 
      System.exit(0); 
     } 
    }); 

    //Adds created objects to GUI 
    file.add(newItem); 
    file.add(saveItem); 
    file.add(loadItem); 
    file.add(exitItem); 
    menubar.add(file); 

    setJMenuBar(menubar); 

} 

}

+0

1)什麼是'BackgroundLoader'? 2)不要將圖像加載到'paint()'或'paintComponent()'3)不要擴展幀。 4)在繪製異步加載的圖像時,使用'ImageObserver'(或使用'ImageIO'加載它)。 5)設置自定義組件的首選大小,而不是框架本身的大小。然後調用'pack()'。 6)爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2012-02-27 23:11:36

回答

4

你應該實現paintComponent()而不是paint()

通過重寫paint而不是委託,你不會讓JFrame繪製它需要繪製的東西。

另外,請看answer

+1

更具體地說 - 他應該爲JPanel或從JComponent派生的其他組件(包括JComponent本身)重寫'paintComponent(...)',然後將其添加到JFrame的contentPane或使其成爲contentPane。 – 2012-02-27 22:33:26