简体   繁体   English

Java mouseClicked显示JFrame在5〜10次之间而不是1次

[英]Java mouseClicked shows JFrame between 5 ~ 10 times instead of 1

I'm trying to code a game for school, for which I have to use a MouseListener. 我正在尝试为学校编写游戏,因此必须使用MouseListener。 I am using the MouseListener for the main menu. 我将MouseListener用于主菜单。 I want the option "Spelregels" (which translates into rules) to create and show a new JFrame containing the rules for the game. 我希望选项“ Spelregels”(转换为规则)创建并显示一个包含游戏规则的新JFrame。

    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. 我想知道为什么它多次显示JFrame。 Like so: http://puu.sh/7UuJb.jpg . 像这样: http : //puu.sh/7UuJb.jpg

Thanks in advance. 提前致谢。

Best Regards. 最好的祝福。

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. 不必在需要重绘组件的任何时候在paintComponent中添加鼠标侦听器,而应在构造函数中添加它。 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
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM