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