的Java Swing:如何顺利动画/移动部件 [英] Java Swing: how to smoothly animate/move component

查看:221
本文介绍了的Java Swing:如何顺利动画/移动部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何动画Swing组件从点A走到B点。下面是这使得自左向右红色的JPanel举动code的宝贝例如:

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();

            }
        });


    }

}

它工作正常,这只是我看起来有点波涛汹涌。用拖动方为类似的例子运动(见所以我认为应该有一个方法,使这更好​​看的Java Swing的可拖动组件)显示更加平滑。任何建议将是多少AP preciated。

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库一个棘手的区域。然而,没有什么是不可能的。您可以使用定时器,例如动画,但我真的建议你不要做。所以,你可以移动部件尽可能最好的,我建议你利用时间框架的库。

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启动一个应用程序之前进行很多测试。

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什么可以做,下载演示程序合奏(搜索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 (Search for "JavaFX Demos and Samples Downloads"). As a source of study, start here.

如果这个选择是对你来说太费力,请我给大家介绍一下时间框架库的链接。在那里,你会发现,做各种事情摆流畅的动画与高性能的Java code的例子。要了解如何使用这个库,我建议你能找到这本书巨富客户端,由切特·哈泽和罗曼盖伊写。虽然书是过时,事情已经在库code被改变,就可以得到更新上的图书馆网站。正如我刚才所说,下载库,并且还下载code样品。随着时间的推移,你最终会做你想要什么的最好的方式。

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:如何顺利动画/移动部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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