0

我想知道是否有任何库或其他东西可以像 BUZZ 一样为 JFrame 设置动画!yahoo messenger中的动画,如果没有现有的库可以做到这一点,那么可能的算法是什么?

4

3 回答 3

3

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

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

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);
            }
        });
    }
}
于 2013-01-23T20:37:58.987 回答
2

看一眼

您将需要评估每个,以确定最能满足您的需求

于 2013-01-23T19:51:04.103 回答
0

我用一个简单的功能做到了这一点:

public void ZUMBIDO() {
        toFront();

        new Thread(new Runnable() {

            int milisegundos = 50;
            int posiciones = 20;

            public void arriba() {

                Point pos = getLocation();
                pos.translate(0, posiciones);
                setLocation(pos);

            }

            public void abajo() {
                Point pos = getLocation();
                pos.translate(0, -posiciones);
                setLocation(pos);
            }

            public void derecha() {
                Point pos = getLocation();
                pos.translate(posiciones, 0);
                setLocation(pos);
            }

            public void izquierda() {
                Point pos = getLocation();
                pos.translate(-posiciones, 0);
                setLocation(pos);
            }

            public void proceso() {
                try {
                    arriba();
                    Thread.sleep(milisegundos);
                    derecha();
                    Thread.sleep(milisegundos);
                    abajo();
                    Thread.sleep(milisegundos);
                    izquierda();
                    Thread.sleep(milisegundos);
                } catch (InterruptedException ex) {
                    Logger.getLogger(cliente.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            @Override
            public void run() {

                for (int i = 0; i <= 5; i++) {
                    proceso();
                }

            }

        }).start();

    }

您可以使用下一个代码观看它的实际操作,它是一个带有回声概念的简单聊天。

https://github.com/MikeSoft/Sistemas_Operativos_Clase/blob/master/chat/src/sistemas/operativos/cliente.java

于 2015-03-30T16:09:32.533 回答