Java Swing: how to smoothly animate/move component(Java Swing:如何平滑地动画/移动组件)
问题描述
我试图弄清楚如何为摆动组件设置动画以从 a 点移动到 b 点.这是一个使红色 JPanel 从左向右移动的代码示例:
I am trying to figure out how to animate a swing component to go from point a to point b. Here is a baby example of code which makes a red JPanel move from left to right :
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MovingSquareExample {
private static final JPanel square = new JPanel();
private static int x = 20;
public static void createAndShowGUI(){
JFrame frame = new JFrame();
frame.getContentPane().setLayout(null);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(square);
square.setBounds(20,200,100,100);
square.setBackground(Color.RED);
Timer timer = new Timer(1000/60,new MyActionListener());
timer.start();
frame.setVisible(true);
}
public static class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
square.setLocation(x++, 200);
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
createAndShowGUI();
}
});
}
}
效果很好,只是我看起来有点波涛汹涌.带有可拖动方块的类似示例的动作(请参阅 Draggable Components inJava Swing) 看起来更流畅,所以我相信应该有办法让它看起来更好.任何建议将不胜感激.
It works fine, it's just that I looks a little choppy. The motion for the analogous example with a draggable square (see Draggable Components in Java Swing) appears much smoother so I believe there should be a way to make this look better. Any suggestions would be much appreciated.
推荐答案
您正在进入 Swing 库的一个棘手领域.然而,没有什么是不可能的.你可以使用 Timer 创建这样的动画,但我真的不建议你这样做.所以你可以尽可能地移动组件,我建议你使用 Timing Framework 图书馆.
You are entering a tricky area for the Swing library. However, nothing is impossible. You can create such animation using Timer, but I really recommend you do not do it. So you can move components as best as possible, I suggest you make use of the Timing Framework library.
但请注意:移动组件不是不经研究就可以完成的.开发了 Swing 布局,以便按特定顺序放置组件.如果您操纵组件的尺寸值和位置,您将破坏布局的功能,并且您的程序可能会以奇怪的方式运行.我曾有过在 Swing 中开发应用程序而不使用布局的情况.在操作系统中,我的程序似乎可以正常运行,但是将其移植到其他系统时,一切都变得混乱.因此,在 Swing 中启动具有此类自定义功能的应用程序之前,您需要密切关注并执行许多测试.
But be aware: Move components is not something that should be made without study. Swing layouts were developed so that the components are placed in a specific order. If you manipulate the values of dimensions and positioning of components, you will be breaking the functionality of the layouts, and your program is likely to behave in strange ways. I've had cases where I developed an application in Swing without the use of layout. In an operating system, my program seemed to work properly, but porting it to other systems, everything went into disarray. Therefore, you need to stay tuned and perform many tests before launching an application in Swing that has such customizations.
这是 JavaFX 技术进入我们手中的原因之一.有了这样的技术,我们可以少做一些事情(在不同的程序中部署应用程序),做更多的事情(包括你遇到问题的那个).考虑迁移到这项技术.所以你看看JavaFX能做什么,下载演示程序合奏.如果您对这项技术感兴趣,我建议您开始学习它这里.如果您不想下载演示,也可以在演示其工作原理的互联网.
This was one reason that the JavaFX technology came into our hands. With such technology, we can concern ourselves with less stuff (deploy the application in different programs) and do much more (including the one you're having trouble). Consider migrating to this technology. So you see what JavaFX can do, download the demo program Ensemble. If you gain interest in this technology, I suggest you start learning it here. If you don't want to download the demo, you can also find videos on the internet that demonstrate how it works.
如果这个替代方案对你来说太费力了,请查看我给你的关于 Timing Framework 库的链接.在那里,您将找到 Java 代码示例,这些代码可以在各种 Swing 事物上以高性能制作流畅的动画.要了解如何使用这个库,我建议您阅读 Chet Haase 和 Romain 编写的 Filthy Rich Clients 一书家伙.虽然这本书已经过时并且库代码中的内容已经更改,但您可以在 图书馆网站.正如我之前所说,下载库,并下载代码示例.随着时间的推移,你最终会以最好的方式做你想做的事.
If this alternative is too laborious for you, check out the link I gave you about the Timing Framework library. There you will find examples of Java code that make smooth animations on various Swing things with a high performance. To learn how to use this library, I suggest you to get the book Filthy Rich Clients, written by Chet Haase and Romain Guy. Although the book is out of date and things have been changed in the library code, you can get updated on the library website. As I said earlier, download the library, and also download the code samples. With time, you will end up doing what you want in the best possible way.
我希望你能完成你想要的.祝你好运.:)
I hope you can accomplish what you want. Good luck. :)
这篇关于Java Swing:如何平滑地动画/移动组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java Swing:如何平滑地动画/移动组件
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01