how to put a background image in a JPanel
hi. this is my code for the startMenu of my memory game. how can i put a backgroundimage in this? thank you
Code:
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
//import static java.util.Collections.*;
class GameInterface extends JFrame implements ActionListener{
private JButton startBtn, exitBtn, howtoBtn, scoreBtn, levelBtn;
private JPanel wholeMenuPanel, firstMenuPanel, secondMenuPanel;
private JLabel welcomeLabel, nameLabel;
private TextField nameField;
public GameInterface(){
init();
MenuPanel();
firstMenuPanel();
secondMenuPanel();
setTitle("GuessMe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700,500);
setVisible(true);
}
public void init(){
try{
jbInit();
}
catch(Exception e){
e.printStackTrace();
}
}
private void jbInit()throws Exception {
startBtn = new JButton ("Start/New Game");
startBtn.addActionListener(this);
howtoBtn = new JButton ("How to play this game");
howtoBtn.addActionListener(this);
levelBtn = new JButton ("Level");
levelBtn.addActionListener(this);
scoreBtn = new JButton ("Score");
scoreBtn.addActionListener(this);
exitBtn = new JButton ("Exit/Quit Game");
exitBtn.addActionListener(this);
welcomeLabel.setBounds(new Rectangle(21, 13, 118, 30));
nameLabel.setBounds(new Rectangle(21, 50, 118, 30));
welcomeLabel.setText("WELCOME!");
nameLabel.setText("Your name here:");
nameField = new TextField("", 20);
//welcomeLabel.addAcionListener(this);
//nameLabel.addActionListener(this);
nameField.addActionListener(this);
}
public void MenuPanel(){
wholeMenuPanel = new JPanel();
firstMenuPanel = new JPanel();
secondMenuPanel = new JPanel();
wholeMenuPanel.setLayout(new BorderLayout());
firstMenuPanel.setLayout(new FlowLayout());
secondMenuPanel.setLayout(new FlowLayout());
wholeMenuPanel.add(firstMenuPanel, BorderLayout.NORTH);
wholeMenuPanel.add(secondMenuPanel, BorderLayout.CENTER);
}
public void firstMenuPanel(){
Panel strtPnl = new Panel();
strtPnl.setLayout(null);
Panel buttonPnl = new Panel();
buttonPnl.add(startBtn);
buttonPnl.add(exitBtn);
buttonPnl.add(howtoBtn);
buttonPnl.add(levelBtn);
buttonPnl.add(scoreBtn);
add(strtPnl, BorderLayout.CENTER);
add(buttonPnl, BorderLayout.SOUTH);
}
public void secondMenuPanel(){
JLabel welcomeLabel = new JLabel();
JLabel nameLabel = new JLabel();
TextField nameField = new TextField();
//backgroundLabel = new JLabel ();
//add(welcomeLabel, FlowLayout.CENTER);
//add(nameLabel, FlowLayout.CENTER);
//add(nameField, TextLayout.CENTER);
}
public void actionPerformed(ActionEvent e){
if (startBtn == e.getSource()){
//GuessMe application = new GuessMe();
}
if (exitBtn == e.getSource()){
System.exit(0);
}
if (howtoBtn == e.getSource()){
JOptionPane pane = new JOptionPane();
pane.showMessageDialog(null, "To play this game, the player shoud click the cards and match up the pairs.");
}
if (levelBtn == e.getSource()){}
if (scoreBtn == e.getSource()){}
}
public static void main(String[] args){
GameInterface Application = new GameInterface();
}
}