简体   繁体   中英

Java Swing - Moving JLabels using Swing Timer class

I'm trying to move these two JLabel objects ( label_2 and label_3 ) according to the mouse movement( when mouse entered and exited). In detail, my goal is when mouse hover overs the label_1 then label_2 and label_3 should move to the specific coordinates. When the mouse is exited, those two labels ( label_2 and label_3 ) should wait for some short amount of time and then reversely perform the animation what they have done in the first animation.

However, the problem that I've encountered is, whenever mouse exits, instead of waiting, label_2 and label_3 objects oscillates between their initial position and final position. How to fix that issue?

Here's the part of my code ( sorry the actual code is quite long ):

         label_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                Point inputDest = new Point( 76, 111);
                Point toolDest = new Point( 172, 24);

                timer = new Timer( 10, new ActionListener(){

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Point pointKeyboard = label_2.getLocation();
                        Point pointTool = label_3.getLocation();

                        if( pointTool.x != 172 )
                            pointTool.x -= 7;
                        if( pointTool.y != 24 )
                            pointTool.y -= 12;
                        if( pointKeyboard.x != 76 )
                            pointKeyboard.x -= 14;
                        if( pointKeyboard.y != 111 )
                            pointKeyboard.y -= 3;
                        label_2.setLocation(pointKeyboard);
                        label_3.setLocation(pointTool);
                        repaint();
                    }
                });
                timer.start();
                if( label_2.getLocation() == inputDest && label_3.getLocation() == toolDest )
                    timer.stop();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {

                Point inputDest = new Point( 174, 132);
                Point toolDest = new Point( 221, 108);

                timer = new Timer( 10, new ActionListener(){

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Point pointKeyboard = label_2.getLocation();
                        Point pointTool = label_3.getLocation();

                        if( pointTool.x != 221 )
                            pointTool.x += 7;
                        if( pointTool.y != 108 )
                            pointTool.y += 12;
                        if( pointKeyboard.x != 174 )
                            pointKeyboard.x += 14;
                        if( pointKeyboard.y != 132 )
                            pointKeyboard.y += 3;
                        label_2.setLocation(pointKeyboard);
                        label_3.setLocation(pointTool);
                        repaint();
                    }
                });
                timer.start();
                if( label_2.getLocation() == inputDest && label_3.getLocation() == toolDest )
                    timer.stop();
            }
        });

Thank you.

You shouldn't use Thread.sleep() and Timer on the EDT. Code by timer also isn't executed on the EDT so that will also cause problems. It will lead to the unpredictable behavior your seeing. I suggest taking aa look at the concurrency in Swing tutorial: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM