Question

I'm trying to code a game for school, for which I have to use a MouseListener. I am using the MouseListener for the main menu. I want the option "Spelregels" (which translates into rules) to create and show a new JFrame containing the rules for the game.

    import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class MouseInput implements MouseListener {

    Main mainMenu = new Main();

    @Override
    public void mouseClicked(MouseEvent e) {
        int mx = e.getX();
        int my = e.getY();

//      public Rectangle startNieuwSpelButton = new Rectangle(100, 275, 250, 50);
//      public Rectangle hervatSpelButton = new Rectangle(100, 340, 250, 50);
//      public Rectangle spelregelsButton = new Rectangle(100, 405, 250, 50);
//      public Rectangle highscoresButton = new Rectangle(100, 470, 250, 50);

        // Start Nieuw Spel Button
        if(mx >= 100 && mx <= 350) {
            if (my >= 275 && my <= 325) {
                System.out.println("Je hebt op de startNieuwSpelButton geklikt.");
            }
        }

        // Hervat Spel Button
        if(mx >= 100 && mx <= 350) {
            if (my >= 340 && my <= 390) {
                System.out.println("Je hebt op de hervatSpelButton geklikt.");
            }
        }

        // Spelregels Button
        if(mx >= 100 && mx <= 350) {
            if (my >= 405 && my <= 455) {
                JFrame spelregelFrame = new JFrame("Lunar Lockout Spelregels");
                spelregelFrame.setSize(450, 650);
                spelregelFrame.setLocation(800, 100);
                spelregelFrame.setVisible(true);

            }
        }

        // Highscore Button
        if(mx >= 100 && mx <= 350) {
            if (my >= 470 && my <= 520) {
                System.out.println("Je hebt op de highscoreButton geklikt.");
            }
        }

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

}

That's the code that I'm using. I was wondering why it's showing the JFrame multiple times. Like so: http://puu.sh/7UuJb.jpg .

Thanks in advance.

Best Regards.

Was it helpful?

Solution

Instead of adding your mouse listener in paintComponent, which is called any time the component needs to be repainted, you should add it in the constructor. That way your panel will only have one mouse listener.

As it stands you add a new one every time you repaint, so they'll keep building up.

So, basically:

public class HoofdMenu extends JPanel {
  // ... member vars

  public HoofdMenu() {
    this.addMouseListener(new MouseInput());
  }

  @Override
  public void paintComponent(Graphics g) {
    // ... don't add listener in here
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top