如何在Java / Swing中使用JLabel制作动画? [英] How to make animation using JLabel in Java/Swing?

查看:101
本文介绍了如何在Java / Swing中使用JLabel制作动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java课程的最终项目,它是Frog跳跃游戏。以下是描述它的视频:

I have a final project of a Java course, it is Frog jump game. Here is the video to describe it :

https://www.youtube.com/watch?v=NxWWP9cK24o

我不知道如何使用Timer,我只知道Thread。睡觉。但它没有用。

I don't know how to use Timer, I only know Thread.sleep. But it's not working.

这里我的代码:

import java.awt.event.ActionListener;
import javax.swing.Timer;




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author gamekvnn
 */
public class FrogGame extends javax.swing.JFrame {
    /**
     * Creates new form ba
     */

    public FrogGame() {
        initComponents();
    }
        Timer time;
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        frog1 = new javax.swing.JLabel();
        frog2 = new javax.swing.JLabel();
        frog3 = new javax.swing.JLabel();
        fr0g2 = new javax.swing.JLabel();
        fr0g1 = new javax.swing.JLabel();
        fr0g3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        background = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(null);

        frog1.setIcon(new javax.swing.ImageIcon("/home/gamekvnn/NetBeansProjects/Project/src/frog1.png")); // NOI18N
        jPanel1.add(frog1);
        frog1.setBounds(10, 310, 80, 80);

        frog2.setIcon(new javax.swing.ImageIcon("/home/gamekvnn/NetBeansProjects/Project/src/frog1.png")); // NOI18N
        jPanel1.add(frog2);
        frog2.setBounds(110, 310, 80, 80);

        frog3.setIcon(new javax.swing.ImageIcon("/home/gamekvnn/NetBeansProjects/Project/src/frog1.png")); // NOI18N
        jPanel1.add(frog3);
        frog3.setBounds(210, 310, 80, 80);

        fr0g2.setIcon(new javax.swing.ImageIcon("/home/gamekvnn/NetBeansProjects/Project/src/frog2.png")); // NOI18N
        jPanel1.add(fr0g2);
        fr0g2.setBounds(510, 310, 80, 80);

        fr0g1.setIcon(new javax.swing.ImageIcon("/home/gamekvnn/NetBeansProjects/Project/src/frog2.png")); // NOI18N
        jPanel1.add(fr0g1);
        fr0g1.setBounds(610, 310, 80, 80);

        fr0g3.setIcon(new javax.swing.ImageIcon("/home/gamekvnn/NetBeansProjects/Project/src/frog2.png")); // NOI18N
        jPanel1.add(fr0g3);
        fr0g3.setBounds(410, 310, 80, 80);

        jButton1.setText("Start");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(520, 20, 100, 29);

        background.setIcon(new javax.swing.ImageIcon("/home/gamekvnn/NetBeansProjects/Project/src/UZKEjzG.png")); // NOI18N
        jPanel1.add(background);
        background.setBounds(0, 0, 710, 440);
        background.getAccessibleContext().setAccessibleName("background");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 708, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:

        frog3.setLocation(frog3.getX()+100, frog3.getY());
        repaint();
        try {
            Thread.sleep(100);;                 //1000 milliseconds is one second.
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        fr0g3.setLocation(fr0g3.getX()-200, fr0g3.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        fr0g2.setLocation(fr0g2.getX()-100, fr0g2.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        frog3.setLocation(frog3.getX()+200, frog3.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        frog2.setLocation(frog2.getX()+200, frog2.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        frog1.setLocation(frog1.getX()+100, frog1.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        fr0g3.setLocation(fr0g3.getX()-200, fr0g3.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        fr0g2.setLocation(fr0g2.getX()-200, fr0g2.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        fr0g1.setLocation(fr0g1.getX()-200, fr0g1.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        frog3.setLocation(frog3.getX()+100, frog3.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        frog2.setLocation(frog2.getX()+200, frog2.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        frog1.setLocation(frog1.getX()+200, frog1.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        fr0g2.setLocation(fr0g2.getX()-100, fr0g2.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        fr0g1.setLocation(fr0g1.getX()-200, frog1.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        frog1.setLocation(frog1.getX()+100, frog1.getY());
        repaint();
        try {
            Thread.sleep(100);;
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrogGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrogGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrogGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrogGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrogGame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JLabel background;
    private javax.swing.JLabel fr0g1;
    private javax.swing.JLabel fr0g2;
    private javax.swing.JLabel fr0g3;
    private javax.swing.JLabel frog1;
    private javax.swing.JLabel frog2;
    private javax.swing.JLabel frog3;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration                   
}


推荐答案


,我只知道Thread.sleep

, i only know Thread.sleep

那么因为代码不行正在事件调度线程(EDT)上执行,这是重新绘制GUI的线程。因此,如果你一直告诉它睡觉,永远不会有机会重新粉刷自己。

Well that won't work because the code is executing on the Event Dispatch Thread (EDT) which is the Thread that repaints the GUI. So if you keep telling it to sleep in never gets a chance to repaint itself.

因此,如果你想使用Thread.sleep,你需要启动一个单独的Thread。

So if you want to use Thread.sleep, you need to start a separate Thread.

阅读并发以获取更多信息。也许您也可以使用工作线程教程中讨论的SwingWorker。

Read the section from the Swing tutorial on Concurrency for more information. Maybe you could also use a SwingWorker as discussed in the tutorial for the worker Thread.

这篇关于如何在Java / Swing中使用JLabel制作动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆