gordsmash 0 Newbie Poster

Well basically im creating a game for a school project and I am coding the Main Menu of the game right now but im having some very strange troubles when removing a JPanel and then replacing it with another. Well this actually works when I use a button to switch the JPanel out with an ActionListener, but later on I switched to a JLabel with a MouseListener so I could make cool looking buttons. Upon doing so the same code I had for the JButton doesnt exactly work for the JLabel. Well heres my code:

package javavideogame;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Cursor;

public class MainMenu extends JPanel implements ActionListener, MouseListener
{
    private JButton btnNewGame;
    private JLabel lblNewGame;

    private Image img;
    private Image background;
    private Timer timer;

    public MainMenu()
    {
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
        setLayout(null);
        setDoubleBuffered(true);
        img = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/com/game/images/logoBlack.png"));
        background = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/com/game/images/mainMenuBackground.png"));
        timer = new Timer(100, this);
        timer.start();

        lblNewGame = new JLabel();
        
        lblNewGame.setLocation(310, 130);
        lblNewGame.setSize(150, 40);
        lblNewGame.setIcon(new ImageIcon(getClass().getResource("/com/game/images/btnBack.png")));
        lblNewGame.setCursor(new Cursor(Cursor.HAND_CURSOR));
        lblNewGame.addMouseListener(this);
        add(lblNewGame);
        


        btnNewGame = new JButton();

        btnNewGame.setText("New Game");
        btnNewGame.setSize(150, 40);
        btnNewGame.setLocation(350, 130);
        add(btnNewGame);

        btnNewGame.addActionListener(this);
       
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.drawImage(background, 0, 0, this);
        g.drawImage(img, 275, 10, null);
        g.setFont(new Font("Arial", Font.BOLD, 22));
        g.drawString("Version 0.1", 10, 25);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == btnNewGame)
        {
            Game newGame = new Game();

            this.setVisible(false);
            timer.removeActionListener(this);
            Main.mainGame.add(newGame);
            this.removeAll();
            Main.mainGame.remove(this);
            Main.mainGame.validate();
        }
        repaint();
        System.out.println("hello");
    }

    public void mouseExited(MouseEvent e)
    {
        lblNewGame.setIcon(new ImageIcon(getClass().getResource("/com/game/images/btnBack.png")));
    }
    public void mouseReleased(MouseEvent e)
    {

    }
    public void mouseClicked(MouseEvent e)
    {
        if(e.getSource() == lblNewGame)
        {
            Game newGame = new Game();

            this.setVisible(false);
            timer.removeActionListener(this);
            Main.mainGame.add(newGame);
            this.removeAll();
            Main.mainGame.remove(this);
            Main.mainGame.validate();
        }
    }
    public void mousePressed(MouseEvent e)
    {

    }
    public void mouseEntered(MouseEvent e)
    {
        if(e.getSource() == lblNewGame)
        {
            lblNewGame.setIcon(new ImageIcon(getClass().getResource("/com/game/images/btnBack1.png")));
        }
    }
}

Sorry for the long code. But its weird because when I use the JButton w/ ActionListener the old panel is removed and the new panel comes up and I can move my character around with the KeyListener. Although if I remove the JButton code and use the JLabel w/ MouseListener the old panel is removed and the new panel comes up but i cant move my character around with the KeyListener. But if I keep the JButton code but dont show it and use the JLabel then the next panel works w/ the KeyListener works

Yes this sounds very confusing probably but I have no idea why its doing this. I just want to remove all the Jbutton code and stick strictly with the JLabel as the new button. Is there a piece of code im missing?

P.S. Yes I know my codes not the best :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.