如何使用NetBeans从Jframe的一侧到另一侧对jLabel进行动画处理 [英] How to animate jLabel from one side to another side of Jframe using netbeans

查看:146
本文介绍了如何使用NetBeans从Jframe的一侧到另一侧对jLabel进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个小应用程序.在我的应用程序中,我有jLabel1Jbutton1.我想使用jButton单击从一侧到另一侧制作jLabel1动画.我不知道如何在jButton1ActionPerformed调用来创建jLabel1的动画.我已经完成了一个绘画应用程序代码,如下所示.

I want to create a small application.In my application I have jLabel1 and Jbutton1. I want to animate jLabel1 from one side to another side using jButton click. I don't know how to call it in the jButton1ActionPerformed to create the animation of jLabel1. I have done a paint application code which is giving as following.

这是我的代码:

public void paint(Graphics g)
{
    super.paint(g);
    Graphics2D g2=(Graphics2D)g;

   g2.drawString("ancd", x, y);
    try {
        Thread.sleep(10000);
    } catch (Exception e) {
        System.out.println(""+e);
    }
    x+=10;
    if(x>this.getWidth())
            {
               x=0;
            }
    repaint();
}

推荐答案

为简单起见,您可以为动画使用Swing计时器.但是,如果我是您,则不会移动JLabel.但是相反,我将直接绘制到JPanel上并保留一组位置(图像的x和y).在计时器中,更新位置并重新粉刷.

To keep things simple, you may use the Swing timer for the animation. However, if I were you I wouldn't move a JLabel. But instead I will draw directly onto the JPanel and keep a set of positions (x and y for the image). In the timer, update the position and repaint it.

由于您想在屏幕上移动JLabel,因此可以执行以下操作:

Since you wanted to move a JLabel across the screen, you can do something like this:

class DrawingSpace extends JPanel{

    private JLabel label;
    private JButton button;
    private Timer timer;    

    public DrawingSpace(){
        setPreferredSize(new Dimension(200, 300));
        initComponents();
        add(label);
        add(button);    
    }

    public void initComponents(){
        label = new JLabel("I am a JLabel !");
        label.setBackground(Color.YELLOW);
        label.setOpaque(true);
        button = new JButton("Move");

        //Move every (approx) 5 milliseconds        
        timer = new Timer(5, new ActionListener(){  
            @Override
            public void actionPerformed(ActionEvent e){
                //Move 1 px everytime
                label.setLocation(label.getLocation().x, label.getLocation().y+1);  
            }               
        });     
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                if(!timer.isRunning())
                    timer.start();
                else
                    timer.stop();   
            }   
        }); 
    }
}

然后运行该程序的类:

class Mover{
    public static void main(String[] args){

        SwingUtilities.invokeLater(new Runnable() {     // Run the GUI codes on the EDT
            @Override
            public void run() {
                JFrame frame = new JFrame("Some Basic Animation");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new DrawingSpace());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);             
            }
        }); 
    }
}

如果您打算使用paint()实现,我想您可能应该覆盖Java组件中的paintComponents(Graphics g)而不是paint(Graphics g). 另外,请勿使用Thread.sleep()之类的东西使涂装方法混乱,否则可能会冻结UI.绘画方法应该只包含绘画所需的代码,而不能包含其他任何内容.

If you plan to implement using paint(), I would say you probably should be overriding paintComponents(Graphics g) instead of paint(Graphics g) from the Java Components. Also do not clutter your paint method with things like Thread.sleep() it may freeze your UI. The paint method should only contain codes needed for painting and nothing else.

这篇关于如何使用NetBeans从Jframe的一侧到另一侧对jLabel进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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