Java 如何制作JFrame动画?

Java 如何制作JFrame动画?,java,swing,animation,jframe,Java,Swing,Animation,Jframe,我想知道是否有任何库或其他类似于BUZZ的动画JFrames!yahoo messenger中的动画,如果没有现有的库可以做到这一点,那么实现这一点的可能算法是什么 看一看 您需要对每一项进行评估,以确定哪一项最能满足您的需求 您需要对每一种方法进行评估,以确定哪种方法最能满足您的需要您可以为您正在处理的框架创建自己的Buzz方法。请看下面给出的代码 编辑 根据MadProgrammer和DavidKroukamp的建议,我已更改代码以符合标准。:) import javax

我想知道是否有任何库或其他类似于BUZZ的动画JFrames!yahoo messenger中的动画,如果没有现有的库可以做到这一点,那么实现这一点的可能算法是什么

看一看

您需要对每一项进行评估,以确定哪一项最能满足您的需求


您需要对每一种方法进行评估,以确定哪种方法最能满足您的需要

您可以为您正在处理的框架创建自己的Buzz方法。请看下面给出的代码

编辑 根据MadProgrammer和DavidKroukamp的建议,我已更改代码以符合标准。:)

import javax.swing.JFrame;
导入javax.swing.JButton;
导入javax.swing.SwingUtilities;
导入java.awt.BorderLayout;
导入java.awt.Point;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
类BuzzFrame扩展了JFrame
{
私人JButton buzz=新JButton(“buzz ME!!”);
公共BuzzFrame()
{
超级(“嗡嗡声帧!!”);
}
公共无效准备GUI()
{
addActionListener(新的BuzzActionListener(this));
设置大小(300200);
getContentPane().add(buzz,BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
公共静态void main(字符串st[]
{
SwingUtilities.invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
BuzzFrame bFrame=新BuzzFrame();
bFrame.prepareGUI();
bFrame.setVisible(真);
}
});
}
}
类BuzzActionListener实现ActionListener
{
私有JFrame;
私人点位;
私有int iDisplaceXBy=5;
private int iDisplaceYBy=-10;
公共BuzzActionListener(JFrame)
{
this.frame=frame;
}
@凌驾
已执行的公共无效操作(操作事件evt)
{
currLocation=frame.getLocationOnScreen();
fireBuzzAction();
}
私有void fireBuzzAction()
{
SwingUtilities.invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
点位置1=新点(currLocation.x+iDisplaceXBy,currLocation.y+idisplaceby);
点位置2=新点(currLocation.x-iDisplaceXBy,currLocation.y-idisplaceby);
对于(int i=0;i<20;i++)
{
机架设置位置(位置1);
机架设置位置(位置2);
}
帧设置位置(当前位置);
}
});
}
}

您可以为正在处理的帧创建自己的Buzz方法。请看下面给出的代码

编辑 根据MadProgrammer和DavidKroukamp的建议,我已更改代码以符合标准。:)

import javax.swing.JFrame;
导入javax.swing.JButton;
导入javax.swing.SwingUtilities;
导入java.awt.BorderLayout;
导入java.awt.Point;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
类BuzzFrame扩展了JFrame
{
私人JButton buzz=新JButton(“buzz ME!!”);
公共BuzzFrame()
{
超级(“嗡嗡声帧!!”);
}
公共无效准备GUI()
{
addActionListener(新的BuzzActionListener(this));
设置大小(300200);
getContentPane().add(buzz,BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
公共静态void main(字符串st[]
{
SwingUtilities.invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
BuzzFrame bFrame=新BuzzFrame();
bFrame.prepareGUI();
bFrame.setVisible(真);
}
});
}
}
类BuzzActionListener实现ActionListener
{
私有JFrame;
私人点位;
私有int iDisplaceXBy=5;
private int iDisplaceYBy=-10;
公共BuzzActionListener(JFrame)
{
this.frame=frame;
}
@凌驾
已执行的公共无效操作(操作事件evt)
{
currLocation=frame.getLocationOnScreen();
fireBuzzAction();
}
私有void fireBuzzAction()
{
SwingUtilities.invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
点位置1=新点(currLocation.x+iDisplaceXBy,currLocation.y+idisplaceby);
点位置2=新点(currLocation.x-iDisplaceXBy,currLocation.y-idisplaceby);
对于(int i=0;i<20;i++)
{
机架设置位置(位置1);
机架设置位置(位置2);
}
帧设置位置(当前位置);
}
});
}
}

我使用了一个简单的函数:

public void ZUMBIDO(){
toFront();
新线程(newrunnable()){
int-milisegundos=50;
int posiciones=20;
公共空间{
点pos=getLocation();
pos.translate(0,posiciones);
设置位置(pos);
}
公共无效abajo(){
点pos=getLocation();
pos.translate(0,-posiciones);
设置位置(pos);
}
公共空间{
点pos=getLocation();
pos.translate(posiciones,0);
设置位置(pos);
}
公开无效izquierda(){
点pos=getLocation();
位置转换(-posiciones,0);
设置位置(pos);
}
公共无效程序()
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class BuzzFrame extends JFrame 
{
    private JButton buzz = new JButton("BUZZ ME!!");
    public BuzzFrame ()
    {
        super("BUZZ Frame!!");
    }
    public void prepareGUI()
    {
        buzz.addActionListener(new BuzzActionListener(this));
        setSize(300,200);
        getContentPane().add(buzz,BorderLayout.NORTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String st[])
    {
        SwingUtilities.invokeLater ( new Runnable()
        {
            @Override
            public void run()
            {
                BuzzFrame bFrame = new BuzzFrame();
                bFrame.prepareGUI();
                bFrame.setVisible(true);
            }
        });
    }
}
class BuzzActionListener implements ActionListener
{
    private JFrame frame;
    private Point currLocation;
    private int iDisplaceXBy = 5;
    private int iDisplaceYBy = -10;
    public BuzzActionListener(JFrame frame)
    {
        this.frame = frame;
    }
    @Override
    public void actionPerformed(ActionEvent evt)
    {
        currLocation = frame.getLocationOnScreen();
        fireBuzzAction();
    }
    private void fireBuzzAction()
    {
        SwingUtilities.invokeLater ( new Runnable() 
        {
            @Override
            public void run()
            {
                Point position1 = new Point( currLocation.x + iDisplaceXBy , currLocation.y + iDisplaceYBy );
                Point position2 = new Point( currLocation.x - iDisplaceXBy , currLocation.y - iDisplaceYBy );
                for (int i = 0; i < 20 ; i++)
                {
                    frame.setLocation(position1);
                    frame.setLocation(position2);
                }
                frame.setLocation(currLocation);
            }
        });
    }
}