gpt4 book ai didi

java - MouseListener 类从未被调用?

转载 作者:太空宇宙 更新时间:2023-11-04 12:43:17 26 4
gpt4 key购买 nike

无论任何点击,我在代码段中的 MouseListener 类都不会被调用。我想知道我的 Creature 类中是否还需要任何其他内容,因为仅存在监听器对我来说效果不佳。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Creature
{
private static int catchCounter = 0, missCounter = 0;//Used to track the amount of misses and catches
//----------------------------------------------------
//Listener class to see if the creature was clicked on
//----------------------------------------------------
private class ClickListener implements MouseListener
{
public void mousePressed(MouseEvent event)
{
System.out.println("this works");
if (GamePanel.xContainNeg < event.getX() && event.getX() < GamePanel.xContainPos)
{
if (GamePanel.yContainNeg < event.getY() && event.getY() < GamePanel.yContainPos)
{
catchCounter++;
if (catchCounter == 10)
{
//Ends game if they won
JOptionPane.showMessageDialog(null, "Congratulations! You won!");
GamePanel.timer.stop();
}
else
{
//Continues game if not
GamePanel.timer.restart();
GamePanel.stats.setText("Catches: " + catchCounter + " \tMisses: " + missCounter);
}
}
else
{
missCounter++;
if (missCounter == 10)
{
//Ends game if lost
JOptionPane.showMessageDialog(null, "You missed 3 times! You lose!");
GamePanel.timer.stop();
}
else
{
//continues game if not
GamePanel.timer.restart();
GamePanel.stats.setText("Catches: " + catchCounter + " \tMisses: " + missCounter);
}
}
}
else
{
missCounter++;
if (missCounter == 10)
{
//ends game if lost
JOptionPane.showMessageDialog(null, "You missed 3 times! You lose!");
GamePanel.timer.stop();
}
else
{
//continues game if not
GamePanel.timer.restart();
GamePanel.stats.setText("Catches: " + catchCounter + " \tMisses: " + missCounter);
}
}
}
//Empty methods
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
}
}

最佳答案

这就是你所拥有的一切吗?你的主要方法在哪里?您永远不会将鼠标监听器实现为任何东西,您需要一个组件并添加鼠标监听器,例如如果您有一个 JPanel

JPanel panel = new JPanel();
panel.addMouseListener(//*add mouse listener here*//);

关于java - MouseListener 类从未被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36605491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com