简体   繁体   中英

JPanel/JButtons disappear after JFrame addition of background image

Java newbie here... I am trying to add an image to the background of my JFrame. I already had buttons added to the panel, on that JFrame. When I added the image to the JFrame, my buttons disappeared.

public class BackPanel extends JFrame {

JFrame frame = new JFrame("Blackjack Game");

ImageIcon background;
JLabel backgroundLbl;

JButton newRoundButton;
JButton endButton;

ImageIcon image;

public BackPanel(GameConsole game){

    final GameConsole game1 = game;
    frame.setSize(800, 600);
  //frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   

  //frame.setContentPane(new JLabel(new ImageIcon(getClass().getResource("cardBackground.jpg"))));
    ImagePanel panel = new ImagePanel("cardBackground.jpg");
    panel.setPreferredSize(new Dimension(800, 600));



    //put frame in the middle of the screen
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);


    //create 2 buttons on the panel

    panel.add(endButton);
    panel.add(newRoundButton);
    frame.getContentPane().add(panel, BorderLayout.CENTER);   //add the panel to the frame

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

I have a feeling it has something to do with layout, but when I use other layouts it doesn't seem to help, or it also makes the image disappear.

I have changed the file from "cardBackground.jpg" to "" in order to see if the buttons are somehow underneath the image. They are not.

I have tried to widen the frame to see if the buttons are next to the image but they aren't. (Which wouldn't make sense anyways because they are not on the same panel).

What am I missing on this?

EDIT: Updated code that is not working: Displays buttons but no background image.

import java.awt.Dimension;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class BackPanel{

    public BackPanel(GameConsole game){
        final GameConsole game1 = game;

        JFrame frame = new JFrame("Blackjack Game");
        ImagePanel panel = new ImagePanel("cardBackground.jpg");
        panel.setPreferredSize(new Dimension(800, 600));


        //create 2 buttons on the panel
        JButton newRoundButton= new JButton("Start another round");
        newRoundButton.setPreferredSize(new Dimension(150, 50));
        newRoundButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                game1.userTurn();
            }
        });

        JButton endButton = new JButton("End Game");
        endButton.setPreferredSize(new Dimension(150, 50));
        endButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "You have chosen to quit.\n"
                        + "Thank you for playing Blackjack.");
                System.exit(0);
            }
        });

        panel.add(endButton);
        panel.add(newRoundButton);

        frame.add(panel);
        frame.setSize(800, 600);

        frame.setLocationRelativeTo(null);


        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        frame.setVisible(true);
        }
    }

Instead of adding an image background like this, make a new JPanel class that has an overloaded paintComponent() function where you draw an image. Here is a link that exemplifies this solution:

Adding an Image to a JPanel Background

Happy coding!

Edit:

I whipped up an example in Eclipse and everything worked fine for me. Here is the code I used:

import java.awt.Dimension;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Test {
    public static void main(String[] args) throws IOException {
        JFrame myFrame = new JFrame();

        //Make a background panel
        ImagePanel panel = new ImagePanel("back.jpg");
        panel.setPreferredSize(new Dimension(800, 600));

        //Make buttons
        JButton button1 = new JButton("Button 1");
        JButton button2 = new JButton("Button 2");

        //Add components
        panel.add(button1);
        panel.add(button2);

        myFrame.add(panel);

        //Set window size
        myFrame.setSize(800, 600);

        //Center it
        myFrame.setLocationRelativeTo(null);

        //Make it close normally
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Make it visible (finalization)
        myFrame.setVisible(true);
    }
}

Here is the ImagePanel code from before as well:

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class ImagePanel extends JPanel {

    private Image image = null;

    public ImagePanel(String filename) {
        this.image = new ImageIcon(filename).getImage();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null);
    }
}

And here are the results: http://postimg.org/image/93k3j5f43/

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