gpt4 book ai didi

java - 如何制作JFrame动画?

转载 作者:行者123 更新时间:2023-12-01 17:26:35 26 4
gpt4 key购买 nike

我想知道是否有任何库或其他东西可以像 BUZZ!雅虎信使中的动画,如果没有现有的库可以做到这一点,那么可能的算法是什么?

最佳答案

您可以为您正在处理的框架创建自己的 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);
}
});
}
}

关于java - 如何制作JFrame动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14488019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com