I am trying to add buttons over the images in the panels. I am also trying to switch between the panels. The program is running but when I am clicking the "instructions" button it gives a huge list of errors in the cmd. What is the issue?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JButton;
import javax.swing.JPanel;
public class htw10 extends JFrame
{
final JFrame f=new JFrame("Hunt The Wumpus");
private static final String FIRST_PANEL="first panel";
private static final String SECOND_PANEL="second panel";
private CardLayout cardLayout=new CardLayout();
private JPanel content;
public void start()
{
// Create a new panel, make 'content' refer to it
content = new JPanel();
// Set the content pane of the window to the panel we just created
f.setContentPane(content);
// Create a button group and some buttons
// Set the layout of the content panel and add buttons
content.setLayout(new FlowLayout());
// Create and add the intro panel and instruction panel to the content panel
content.add(introPanel(),FIRST_PANEL);
content.add(instructionPanel(),SECOND_PANEL);
f.setSize(750,500);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
f.setVisible(true);
}
private JPanel instructionPanel()
{
JPanel secondPanel=new JPanel();
ImageIcon icon=new ImageIcon("img2.jpg");
JLabel pic2 = new JLabel(icon);
secondPanel.add(pic2);
JButton b1=new JButton("Back");
content.add(b1);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(content,FIRST_PANEL);
}
});
secondPanel.repaint();
return secondPanel;
}
public JPanel introPanel()
{
JPanel iPanel=new JPanel();
ImageIcon icon=new ImageIcon("img1.jpg");
JLabel picLabel = new JLabel(icon);
iPanel.add(picLabel);
ButtonGroup group=new ButtonGroup();
JButton b1=new JButton("Instructions");
JButton b2=new JButton("Play");
JButton b3=new JButton("Exit");
picLabel.add(b1);
//f.getContentPane().add(picLabel,BorderLayout.SOUTH);
content.add(b1);
content.add(b2);
content.add(b3);
// Add a listener to the 'Instructions' button
// so that the cardLayout is shown when the button is clicked
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardLayout.show(content,SECOND_PANEL);
}
});
iPanel.repaint();
return iPanel;
}
public static void main(String args[])throws Exception
{
htw10 obj=new htw10();
obj.start();
}
}