我正在开发一款需要鼠标输入的游戏,我通过创建一个扩展MouseAdapter的Mouse类来完成Key Key的操作。通过单击窗口进行测试时,它什么也没做。代码如下。任何帮助表示赞赏。
主类
import javax.swing.JFrame;
public class TopDownShooter {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("Game");
frame.setSize(1360,680);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.add(new GameFrame());
frame.setVisible(true);
}
}
MouseAdapt Class
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MouseAdapt extends MouseAdapter {
public void MouseClicked(MouseEvent e)
{
System.out.print("Mouse Clicked");
}
}
GameFrame类
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class GameFrame extends JPanel implements ActionListener{
Timer mainTimer;
public GameFrame()
{
setFocusable(true)
addMouseListener(new MouseAdapt());
mainTimer = new Timer(10,this);
mainTimer.start();
}
public void actionPerformed(ActionEvent arg0)
{
}
}
答案 0 :(得分:0)
MouseClicked
不存在于MouseListener
界面
当您认为重写方法时,您应该始终使用@Override
注释,因为如果出现问题(如拼写错误)会产生编译时错误
例如......
public class MouseAdapt extends MouseAdapter {
@Override
public void MouseClicked(MouseEvent e) {
}
}
会失败,但是......
public class MouseAdapt extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
}
}
将编译
请记住,Java区分大小写,很容易犯这些不明显的错误。
您还应该查看How to write a mouse listener和JavaDocs,以确保您在遇到此类问题时正确实施了该功能