添加 JPanel 并向其添加鼠标侦听器时出现问题

发布于 2024-12-08 10:45:12 字数 2099 浏览 2 评论 0原文

我正在制作一个简单的应用程序,其中有一个菜单屏幕和一个游戏屏幕。在菜单屏幕中有一个执行按钮,如果用鼠标单击它,菜单屏幕会切换到游戏屏幕,其中有一些简单的游戏玩法。

主类(它扩展了 JFrame)管理屏幕切换并通过卡片布局设置屏幕。它在其上设置了一个JFrame和一个名为cardpanel的JPanel。 Cardpanel 然后添加:

cardPanel.add(menu, "menuscreen");
cardPanel.add(game, "gamescreen");

菜单和游戏是对其类的引用(这些类扩展了 JPanel)。

问题是,当我尝试在菜单中添加注册到鼠标监听器的 JPanel p 时,单击它时没有任何反应。我尝试将 p 直接添加到主类中,并且它有效,因为菜单是默认的可见屏幕,但是当切换到游戏室时,用户键盘输入不起作用。当游戏屏幕是默认屏幕并且 p 直接添加到 main 中时,输入不起作用。所以我真的必须将 p 添加到菜单屏幕中,另外,它使代码看起来更好。

感谢您的帮助和建议!

这是菜单类代码,其中我尝试添加p:(删除了不相关的绘图)

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;



public class Menu extends JPanel implements ActionListener, MouseListener, Commons{

    public static int points = 0;
    public int xMouse;
    public int yMouse;
    private Timer timer;
    private Image image;
    private String backMenu = "backMenu.png";
    private Go go;
    private Logo logo;
    JPanel p = new JPanel();
    public Main main;

    public Menu(Main main){
        add(p);
        this.main = main;

                timer = new Timer(5, this);
                timer.start();

                ImageIcon ii = new ImageIcon(getClass().getResource(backMenu));
        image = ii.getImage();
        go = new Go();
        logo = new Logo();
        setVisible(true);

        setFocusable(true);

        p.addMouseListener(this);
            p.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        repaint();;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if ((e.getX() > go.getX()) && (e.getX() < go.getX()+go.getWidth())){
        if ((e.getY() > go.getY()) && (e.getY() < go.getY()+go.getHeight()))

        {
            main.changeScreen();    
        }
        }

    }
}

I´m making a simple application in which there are a menu-screen and a game-screen. In the menu-screen there are a go-button and if you click it with the mouse the menu-screen switches to the game-screen, in which there are some simple gameplay.

The main class (it extends JFrame) manages the screen switching and sets up the screens through cardlayout. It sets up a JFrame and a JPanel called cardpanel on it. Cardpanel then adds:

cardPanel.add(menu, "menuscreen");
cardPanel.add(game, "gamescreen");

menu and game are references to their classes (the classes extends JPanel).

The issue is that when I try to add the JPanel p, which is registered to a mouselistener, in the menu, nothing happens when clicking on it. I tried to add p directly into the main class and it worked since menu is the default visible screen, but when switching to game-room, the users keyboard inputs didn´t work. And when the game-screen was the default one, and p was added directly into main, the inputs did not work. So I really have to add p into the menu-screen, plus, it makes the code look better.

Thankful for help and advices!

Here is the menu class code, in which I have tried to add p: (deleted irrelevant drawing)

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;



public class Menu extends JPanel implements ActionListener, MouseListener, Commons{

    public static int points = 0;
    public int xMouse;
    public int yMouse;
    private Timer timer;
    private Image image;
    private String backMenu = "backMenu.png";
    private Go go;
    private Logo logo;
    JPanel p = new JPanel();
    public Main main;

    public Menu(Main main){
        add(p);
        this.main = main;

                timer = new Timer(5, this);
                timer.start();

                ImageIcon ii = new ImageIcon(getClass().getResource(backMenu));
        image = ii.getImage();
        go = new Go();
        logo = new Logo();
        setVisible(true);

        setFocusable(true);

        p.addMouseListener(this);
            p.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        repaint();;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if ((e.getX() > go.getX()) && (e.getX() < go.getX()+go.getWidth())){
        if ((e.getY() > go.getY()) && (e.getY() < go.getY()+go.getHeight()))

        {
            main.changeScreen();    
        }
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文