actionListener在调用frm Jframe时在JPanel上执行两次]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了actionListener在调用frm Jframe时在JPanel上执行两次]相关的知识,希望对你有一定的参考价值。

我想给我的吃豆精灵制作动画,但是当我的计时器调用actionperformed来更新图像时,它会执行两次并跳过动画

这是我的主班代码

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.concurrent.TimeUnit; import javax.swing.Timer; public class Pacman extends JFrame implements ActionListener, KeyListener{ public static Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); public static int FRAME_HEIGHT = (int)(dim.height-dim.height/5); public static int FRAME_WIDTH = (int)(dim.width-dim.width/5); public static Pacman frame = new Pacman(); public static Game gameInterface = new Game(); public static Timer timer; public Pacman() { super("Pac-man"); timer = new Timer(175, this); timer.start(); addKeyListener(this); } public static void main(String[] args) { ImageIcon frameIcon = new ImageIcon("Icon.png"); frame.setSize(FRAME_WIDTH,FRAME_HEIGHT); frame.setIconImage(frameIcon.getImage()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setResizable(false); frame.setContentPane(gameInterface); gameInterface.requestFocus(); gameInterface.setFocusable(true); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); int JframeX = (dim.width-FRAME_WIDTH)/2; int JframeY = (dim.height-FRAME_HEIGHT)/2; frame.setLocation(JframeX, JframeY); } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void paint(Graphics g) { super.paint(g); } public void actionPerformed(ActionEvent ev){ repaint(); } }

这是我的游戏类的代码

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.concurrent.TimeUnit; import javax.swing.Timer; public class Game extends JPanel implements ActionListener, KeyListener{ public static Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); public static int FRAME_HEIGHT = (int)(dim.height-dim.height/5); public static int FRAME_WIDTH = (int)(dim.width-dim.width/5); public static Game gamePanel = new Game(); public static Timer timer; public static boolean gameExit = false; public static int animCounter = 0, imgPacmanX1 = 840, imgPacmanY1 = 0, imgPacmanX2 = 890, imgPacmanY2 = 50; public Game() { timer = new Timer(500, this); timer.start(); addKeyListener(this); } public static void main(String[] args) { } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void paintComponent(Graphics g) { super.paintComponent(g); ImageIcon animations = new ImageIcon("Pacman_Animations.png"); g.setColor(Color.BLUE); g.fillRect(0, 0, FRAME_WIDTH, FRAME_HEIGHT); g.drawImage(animations.getImage(), 150, 200, 200, 250, imgPacmanX1, imgPacmanY1, imgPacmanX2, imgPacmanY2, null); } public void actionPerformed(ActionEvent ev){ if(animCounter == 2){ imgPacmanY1 = 0; imgPacmanY2 = 50; animCounter = 0; } else{ imgPacmanY1 += 50; imgPacmanY2 += 50; animCounter += 1; } repaint(); System.out.print(imgPacmanY1 + " " + imgPacmanY2 + " "); } }

如果我单独运行游戏类,则ActionListener可以正常工作,并且只能执行一次,但是从我的主类调用后,actionListener可以执行两次,动画开始跳过帧。

我希望为pacman sprite制作动画,但是当我的计时器调用actionperformed更新图像时,它会执行两次并跳过动画,这是我的主类import java.awt。*的代码; ...

答案
您有两个Timer,每个都触发一个repaint,并且……欢迎来到您的体系结构为何出错的奇妙世界。

以上是关于actionListener在调用frm Jframe时在JPanel上执行两次]的主要内容,如果未能解决你的问题,请参考以下文章

在重定向[重复]之前,PrimeFaces命令按钮未调用actionListener

爪哇摇摆;两个类,在哪里放置 if 语句和新的 actionlistener?

当有人尝试使用 WifiP2PManager.connect(Channel, WifiP2pConfig,ActionListener) 进行连接时,会调用哪个方法?

获取 onVisibilityChange ActionListener

delphi mdi子窗体显示和释放的问题

C#中如何在一个窗体中调用另一个窗体控件中的数据?