I am a beginner at Java. I was doing this assignment on making a game. I already made the command line program and now I am just trying to make GUI for it. So, the problem I got was: when executing this trial program, Jlabel was not added to p2 panel when a button is clicked. Could you please help me figure out what I did wrong? Thanks.
//imports the necessary packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//GUI controls for Mine Tunnel game, buttons and text fields
class RiverGUI extends JPanel implements ActionListener {
private static boolean gameOver;
private JButton clearallButton, wolfButton, goatButton, cabbageButton, resetButton, rowButton;
private JTextField msgField;
private static RiverCrossing mine;
private JPanel p, p1,p2,p3,p4,p5;
private Jlabel wolflabel, goatlabel, cabbagelabel;
// Constructor creates panels, buttons and other GUI components
public RiverGUI() {
if (mine.flag == true)
gameOver = true;
else
gameOver = false;
Color topColour, middleColour, bottomColour;
mine = new RiverCrossing();
topColour = new Color(0,255,0); //creates a variable for color
middleColour = new Color(0,0,255);
bottomColour = new Color(0,255,0);
//Create panels
p = new JPanel(new GridLayout(5,1));
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel(new FlowLayout());
p1.setBackground(topColour);
p2.setBackground(topColour);
p3.setBackground(middleColour);
p4.setBackground(bottomColour);
p5.setBackground(bottomColour);
wolflabel = new JLabel("Wolf");
goatlabel = new JLabel("Goat");
cabbagelabel = new JLabel("Cabbage");
wolflabel.setFont(new Font("Serif", Font.BOLD, 24));
goatlabel.setFont(new Font("Serif", Font.BOLD, 24));
cabbagelabel.setFont(new Font("Serif", Font.BOLD, 24));
//***** set background colour for the panels
p.setPreferredSize(new Dimension(800,600));
p1.setPreferredSize(new Dimension(800,50));
p2.setPreferredSize(new Dimension(800,200));
p3.setPreferredSize(new Dimension(800,300));
p4.setPreferredSize(new Dimension(800,200));
p5.setPreferredSize(new Dimension(800,50));
p2.setBorder(BorderFactory.createRaisedBevelBorder());
p3.setBorder(BorderFactory.createRaisedBevelBorder());
//Create text fields and set properties
msgField = new JTextField("",30);
//Create buttons and set same button size
clearallButton = new JButton("clear all");
wolfButton = new JButton("wolf");
goatButton = new JButton("goat");
cabbageButton = new JButton("cabbage");
resetButton = new JButton("reset");
rowButton = new JButton("row");
//***** Create other buttons outButton, leftButton, rightButton, resetButton, quitButton
//Add components to panels
p.add(p1);
p.add(p2);
p.add(p3);
p.add(p4);
p.add(p5);
p1.add(msgField);
p1.add(Box.createHorizontalStrut(30));
p1.add(resetButton);
p4.add(wolflabel);
p4.add(Box.createHorizontalStrut(100));
p4.add(goatlabel);
p4.add(Box.createHorizontalStrut(100));
p4.add(cabbagelabel);
//***** add msgField, counterField, and resetButton buttons to p1 panel
p5.add(clearallButton);
p5.add(Box.createHorizontalStrut(30));
p5.add(wolfButton);
p5.add(Box.createHorizontalStrut(30));
p5.add(goatButton);
p5.add(Box.createHorizontalStrut(30));
p5.add(cabbageButton);
p5.add(Box.createHorizontalStrut(30));
p5.add(rowButton);
add(p); //add all GUI components
//Add listeners to buttons
resetButton.addActionListener(this);
rowButton.addActionListener(this);
wolfButton.addActionListener(this);
goatButton.addActionListener(this);
cabbageButton.addActionListener(this);
clearallButton.addActionListener(this);
//***** add Listener to five other buttons
}//end of constructor
public void actionPerformed(ActionEvent e) {
msgField.requestFocus();
if (e.getSource() == resetButton) {
msgField.setText("Game restarted...");
}
if (e.getSource() == wolfButton) {
msgField.setText("You clicked on wolf");
p2.add(wolflabel);
p4.remove(wolflabel);
}
}
}