2012-12-27 125 views
1

這是我第一次在stackoverflow上,我遇到了一個我似乎無法理解的問題。我試圖獨立開發一個小型的桌面遊戲,它使用一個名爲「Frame.java」的類來創建一個包含所有菜單的JFrame,並且我需要使用名爲「create()」的方法來返回該框架。我還使用另一個名爲「MainPanel.java」的類,該類擴展了JPanel,以創建一個MainPanel實例以添加到框架的內容窗格。但是,當我在我的小驅動程序中運行所有代碼時,似乎沒有任何顯示。任何幫助,將不勝感激!Java JPanel將不會顯示在JFrame中

public class MainPanel extends JPanel{ 

//the background image of the game 
private BufferedImage img = null; 
//GUI components of the game 
private JPanel gameWindow, gameWindowHolder, gameInfoHolder, LevelPanel, RevenuePanel, 
       ActionPanel, TimePanel; 

public MainPanel(String path, int width, int height){ 

    //create BufferedImage based on path 
    img = new ImageHelper().createBufferedImage(path); 
    //use img to create JPanel gameWindow 
    gameWindow = ImageHelper.makeImageComponent(img, width, height); 

    gameInfoHolder = new JPanel(); 
    gameInfoHolder.setPreferredSize(new Dimension(width+10, height+10)); 
    gameInfoHolder.setBackground(Color.black); 
    gameInfoHolder.add(gameWindow); 

    //set size of this MainPanel 
    setPreferredSize(new Dimension(width+300, height+10)); 
    //add gameInfoHolder to MainPanel 
    add(gameInfoHolder); 

} 

public void paint(Graphics g) { 
    super.paintComponent(g); 

} 
} 

public class Driver { 


public static void main(String []args){ 
    JFrame frame = Frame.create(); 

    JPanel panel = new MainPanel("images/backgrounds/jessicaAlba.jpg", 330, 500); 

    frame.getContentPane().add(panel); 

    frame.pack(); 
    frame.setVisible(true); 
} 

} 

public class ImageHelper { 


// 
//Returns an ImageIcon object based on the path 
// 
public ImageIcon createImageIcon(String path, String description){ 

    URL imgURL = getClass().getResource(path); 
    if (imgURL != null) 
     return new ImageIcon(imgURL, description); 
    else { 
     System.err.println("Couldn't find this file: " + path + ". Check path."); 
     return null; 
    } 
} 

// 
//Returns an Image object based on the path 
// 
public Image createImage(String path){ 

    URL imgURL = getClass().getResource(path); 
    Toolkit kit = Toolkit.getDefaultToolkit(); 

    if (imgURL != null){ 
     return kit.createImage(imgURL); 
    } 
    else { 
     System.err.println("Couldn't find this file: " + path + ". Check path."); 
     return null; 
    } 

} 

// 
//Returns a BufferedImage object based on the path 
// 
public BufferedImage createBufferedImage(String path){ 
    BufferedImage image; 
    URL imgURL = getClass().getResource(path); 
    try 
    { 
     image = ImageIO.read(imgURL); 
    } 
    catch (IOException e) 
    { 
     System.err.println("Couldn't find this file: \""+ path +"\". Check path."); 
     return null; 
    } 
    return image; 
} 

// 
//Returns a JPanel object composed of the image found in the path. 
// 
public static JPanel makeImageComponent(String path, int width, int height){ 
    BufferedImage image; 
    JLabel picLabel; 
    ImageIcon icon; 

    JPanel panel = new JPanel(); 
    JPanel panel2 = new JPanel(); 
    ImageHelper h = new ImageHelper(); 

    image = h.createBufferedImage(path); 
    image = resize(image, width, height); 
    icon = new ImageIcon(image); 

    panel.setPreferredSize(new Dimension(width, height)); 
    panel2.setPreferredSize(new Dimension(width+10, height+10)); 

    panel2.setBackground(Color.black); 
    picLabel = new JLabel(icon); 
    panel.add(picLabel); 
    panel2.add(panel); 

    return panel2; 
} 

// 
//Returns a JPanel object composed of the BufferedImage object in the argument 
// 
public static JPanel makeImageComponent(BufferedImage image, int width, int height){ 
    JLabel picLabel; 
    ImageIcon icon; 

    JPanel panel = new JPanel(); 
    image = resize(image, width, height); 
    icon = new ImageIcon(image); 
    panel.setPreferredSize(new Dimension(width, height)); 
    picLabel = new JLabel(icon); 
    panel.add(picLabel); 

    return panel; 
} 

// 
//Resizes the BufferedImage object to the specified new width and new height. 
// 
public static BufferedImage resize(BufferedImage img, int newW, int newH) { 
    int w = img.getWidth(); 
    int h = img.getHeight(); 
    BufferedImage dimg = new BufferedImage(newW, newH, img.getType()); 
    Graphics2D g = dimg.createGraphics(); 
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
    g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null); 
    g.dispose(); 
    return dimg; 
} 
} 
+2

請學習如何格式化SO的代碼。選擇代碼示例,然後單擊郵件發佈區域上方的「{}」。可能需要在代碼清單之間添加一些普通文本。如果是這樣,請將它作爲課程的標題。爲了更快地提供幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

謝謝我會記住下次 – DarkyTheOdd

+0

不用擔心,我會更深入地考慮下一個問題(當有SSCCE時)。 –

回答

5

沒有什麼作爲你短路與super.paintComponent所以沒有子組件將被塗油漆的 '疊加' 顯示。

public void paint(Graphics g) { 
    super.paintComponent(g); 
} 

由於這確實沒有任何用途,因此可以將其刪除並添加面板。

+0

哇,工作,謝謝! – DarkyTheOdd