Java 为什么不是';跳跃动画不在放映吗?

Java 为什么不是';跳跃动画不在放映吗?,java,swing,animation,game-physics,Java,Swing,Animation,Game Physics,我是一名初级程序员,最近开始尝试用Java制作游戏 它非常基本,不包含任何类(尽管它应该包含),但无论如何,我尝试在JPanel上使用JLabel作为精灵制作跳跃动画,但每当我尝试使用Thread.sleep(毫秒)来计时标签每次移动之间的时间间隔时Java似乎跳过它,将标签移动到最后一个位置 JFrame frame = new JFrame("malario"); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setV

我是一名初级程序员,最近开始尝试用Java制作游戏

它非常基本,不包含任何类(尽管它应该包含),但无论如何,我尝试在
JPanel
上使用
JLabel
作为精灵制作跳跃动画,但每当我尝试使用
Thread.sleep(毫秒)来计时标签每次移动之间的时间间隔时
Java似乎跳过它,将标签移动到最后一个位置

JFrame frame = new JFrame("malario");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

frame.setVisible(true);
frame.setSize(700, 700);
JPanel panel = new JPanel();

panel.setLayout(null);
panel.setBackground(Color.blue);
JLabel malario = new JLabel("Malario");
malario.setOpaque(true);
malario.setBackground(Color.green);
panel.add(malario);

malario.setBounds(100, 550, 50, 50);

JLabel platform = new JLabel();
platform.setOpaque(true);
platform.setBounds(0,600,700,50);
panel.add(platform);
frame.setContentPane(panel);
frame.addKeyListener(new KeyListener() {
    int originalx = 100;
    int originaly = 550;
    int currentlocx = originalx;
    int currentlocy = originaly;

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void keyPressed(KeyEvent e) {

        if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            malario.setBounds(currentlocx+10,currentlocy , 50, 50);
            currentlocx = currentlocx+10;
        }

        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            malario.setBounds(currentlocx-10,currentlocy , 50, 50);
            currentlocx = currentlocx-10;
        }
        int jumpy=0;
        if(e.getKeyCode()==KeyEvent.VK_UP){
            jumpy= currentlocy-100;
            while(jumpy!=currentlocy){

                malario.setBounds(currentlocx,currentlocy-10 , 50, 50);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                currentlocy = currentlocy-10;
            }
        }
    }
});

}
public static int Time(){
    return (int)System.currentTimeMillis();
}
} 
无法使用Thread.sleep()

所有侦听器代码都在事件调度线程(EDT)上执行,EDT是负责处理事件和绘制GUI的线程。因此,当您告诉线程睡眠时,GUI无法重新绘制自身,直到循环中的所有代码都完成执行,因此您只能看到组件位于其最后位置

JFrame frame = new JFrame("malario");
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

frame.setVisible(true);
frame.setSize(700, 700);
JPanel panel = new JPanel();

panel.setLayout(null);
panel.setBackground(Color.blue);
JLabel malario = new JLabel("Malario");
malario.setOpaque(true);
malario.setBackground(Color.green);
panel.add(malario);

malario.setBounds(100, 550, 50, 50);

JLabel platform = new JLabel();
platform.setOpaque(true);
platform.setBounds(0,600,700,50);
panel.add(platform);
frame.setContentPane(panel);
frame.addKeyListener(new KeyListener() {
    int originalx = 100;
    int originaly = 550;
    int currentlocx = originalx;
    int currentlocy = originaly;

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
    }

    @Override
    public void keyPressed(KeyEvent e) {

        if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            malario.setBounds(currentlocx+10,currentlocy , 50, 50);
            currentlocx = currentlocx+10;
        }

        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            malario.setBounds(currentlocx-10,currentlocy , 50, 50);
            currentlocx = currentlocx-10;
        }
        int jumpy=0;
        if(e.getKeyCode()==KeyEvent.VK_UP){
            jumpy= currentlocy-100;
            while(jumpy!=currentlocy){

                malario.setBounds(currentlocx,currentlocy-10 , 50, 50);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                currentlocy = currentlocy-10;
            }
        }
    }
});

}
public static int Time(){
    return (int)System.currentTimeMillis();
}
} 
相反,您需要使用
摆动计时器来安排动画。读这本书。以下章节介绍:

  • Swing中的并发性
    -解释有关EDT的更多信息
  • 如何使用摆动计时器
    -有关使用计时器的示例
  • 了解更多信息

    另外,不要使用KeyListener。相反,最好使用
    键绑定
    。本教程还有一节介绍如何使用键绑定

    编辑:

    请参阅中的
    键盘动画
    示例,以了解显示以下两个方面的工作示例:

  • 如何使用密钥绑定
  • 如何做动画
  • 无法使用Thread.sleep()

    所有侦听器代码都在事件调度线程(EDT)上执行,EDT是负责处理事件和绘制GUI的线程。因此,当您告诉线程睡眠时,GUI无法重新绘制自身,直到循环中的所有代码都完成执行,因此您只能看到组件位于其最后位置

    JFrame frame = new JFrame("malario");
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    
    frame.setVisible(true);
    frame.setSize(700, 700);
    JPanel panel = new JPanel();
    
    panel.setLayout(null);
    panel.setBackground(Color.blue);
    JLabel malario = new JLabel("Malario");
    malario.setOpaque(true);
    malario.setBackground(Color.green);
    panel.add(malario);
    
    malario.setBounds(100, 550, 50, 50);
    
    JLabel platform = new JLabel();
    platform.setOpaque(true);
    platform.setBounds(0,600,700,50);
    panel.add(platform);
    frame.setContentPane(panel);
    frame.addKeyListener(new KeyListener() {
        int originalx = 100;
        int originaly = 550;
        int currentlocx = originalx;
        int currentlocy = originaly;
    
        @Override
        public void keyTyped(KeyEvent e) {
        }
    
        @Override
        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
    
            if(e.getKeyCode()==KeyEvent.VK_RIGHT){
                malario.setBounds(currentlocx+10,currentlocy , 50, 50);
                currentlocx = currentlocx+10;
            }
    
            if(e.getKeyCode()==KeyEvent.VK_LEFT){
                malario.setBounds(currentlocx-10,currentlocy , 50, 50);
                currentlocx = currentlocx-10;
            }
            int jumpy=0;
            if(e.getKeyCode()==KeyEvent.VK_UP){
                jumpy= currentlocy-100;
                while(jumpy!=currentlocy){
    
                    malario.setBounds(currentlocx,currentlocy-10 , 50, 50);
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    currentlocy = currentlocy-10;
                }
            }
        }
    });
    
    }
    public static int Time(){
        return (int)System.currentTimeMillis();
    }
    } 
    
    相反,您需要使用
    摆动计时器来安排动画。读这本书。以下章节介绍:

  • Swing中的并发性
    -解释有关EDT的更多信息
  • 如何使用摆动计时器
    -有关使用计时器的示例
  • 了解更多信息

    另外,不要使用KeyListener。相反,最好使用
    键绑定
    。本教程还有一节介绍如何使用键绑定

    编辑:

    请参阅中的
    键盘动画
    示例,以了解显示以下两个方面的工作示例:

  • 如何使用密钥绑定
  • 如何做动画