简体   繁体   中英

Images in JFrame don't display

I am trying to add images to my program and I have a problem with 2 of them. Ball.png and ground.png don't display.

Where did I make a mistake?

When I add images of ball and ground as a background, there is no problem.

    public class Game extends JPanel implements ActionListener {

Image ball;
Image ground;
Image arrow;

public Game(){
    try {
        ball = ImageIO.read(new File("ball.png"));
    } catch (IOException e) { 
        System.out.println("pliku brak");
    }
    try {
        ground = ImageIO.read(new File("ground.png"));
    } catch (IOException ex) {
        System.out.println("pliku brak");
    } 
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(ball, 100, 100, null);
    g.drawImage(ground, 300, 300, null);
}
@Override
public void actionPerformed(ActionEvent e) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}


public class Start {
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            okienko();
        }
    });
}

public static void okienko()
{    

    JFrame frame = new JFrame("Hold The Ball");

    try
    {
        frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("background.png")))));
    } catch (IOException e)
        {
            System.out.println("pliku brak");
        }
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new Game());
    frame.setSize(1000, 600);
    frame.setResizable(false);
    frame.setVisible(true);       
}
}

Don't try to layer components like this.

The best approach here would be to paint the background at the same time as painting the other game elements. To do that:

  1. Change

     Image arrow; 

    to

     Image arrow; Image bg; // declare the BG as a class attribute of the Game class 
  2. Load the BG at the same time as the other two images.
  3. Paint the BG first (at 0x0) in the paintComponent(Graphics) method.

You cannot see your Game JPanel because your JFrame is filled with the frame.setContentPane(...) Background JLabel . You need to use a LayoutManager to be able to lay your Panel over the other.

 frame.setLayout(new BorderLayout());
 frame.add(new Game(), BorderLayout.CENTER);

Will fix that.
But then you will not see the Background since your Game JPanel is on top of all. To fix that you should make this Panel have an invisible background.
So add

setOpaque(false);

to your Game Constructor.

You should use image path with ImageIcon then setIcon to JLabel.

ImageIcon icon = new ImageIcon("images/background.png"); JLabel label = new JLabel(icon);

You can read this : https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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