简体   繁体   English

Java JPanel将不会显示在JFrame中

[英]Java JPanel will not display in JFrame

This is my first time on stackoverflow and I am having an issue which I can't seem to get to the bottom of. 这是我第一次使用stackoverflow,但遇到了一个似乎无法深入探究的问题。 I am trying to independently develop a small desktop game which uses a class called "Frame.java" to create a JFrame with all the menus and such that I need using a method called "create()", which returns the frame. 我试图独立开发一款小型桌面游戏,该游戏使用名为“ Frame.java”的类来创建具有所有菜单的JFrame,因此我需要使用名为“ create()”的方法来返回框架。 I'm also using another class called "MainPanel.java", which extends JPanel, to create an instance of MainPanel to add to the frame's contentpane. 我还使用了另一个名为“ MainPanel.java”的类,该类扩展了JPanel,以创建MainPanel的实例以添加到框架的contentpane中。 However, when I run all the code in my little driver program, nothing seems to be displaying. 但是,当我在小驱动程序中运行所有代码时,似乎什么也没显示。 any help would be appreciated! 任何帮助,将不胜感激!

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;  
}
}

Nothing is displayed as you're short-circuiting the paint 'stack' with super.paintComponent so no child components will be painted. 当您用super.paintComponent短路绘画“堆栈”时,没有任何显示,因此不会绘制任何子组件。

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

As this is really serving no purpose it can be removed and the added panels will be displayed. 由于这实际上没有任何作用,因此可以将其删除并显示添加的面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM