简体   繁体   中英

Java Swing: Animations made using Thread.sleep() not working as expected

I am trying to create a sliding menu bar in which the menu bar having initial width of 60px, which grows in width to 260px on the ActionPerformed event of the menu icon button (three small horizontal lines Menu Icon ) with the following code:

private void menuLabelBtnActionPerformed(java.awt.event.ActionEvent evt) {                                             
        if(menuToggle.isSelected()){
            menuToggle.setSelected(false);
            Thread th = new Thread(){
                public void run(){
                    try{
                        for(int i=259;i>=210;i=i-15){
                            Thread.sleep(1);
                            i--;
                            slideMenuPane.setBounds(slideMenuPane.getX(), slideMenuPane.getY(), i, slideMenuPane.getHeight());
                        }
                        for(int i=210;i>=110;i=i-35){
                            Thread.sleep(1);
                            i--;
                            slideMenuPane.setBounds(slideMenuPane.getX(), slideMenuPane.getY(), i, slideMenuPane.getHeight());
                        }
                        for(int i=110;i>=60;i=i-5){
                            Thread.sleep(1);
                            i--;
                            slideMenuPane.setBounds(slideMenuPane.getX(), slideMenuPane.getY(), i, slideMenuPane.getHeight());
                        }
                    }catch(Exception e){
                        System.out.println(e);      
                    }
                }
            };
            th.start();
        }
        else{
            menuToggle.setSelected(true);
            Thread th = new Thread(){
                public void run(){
                    try{
                        for(int i=61;i<=110;i=i+15){
                            Thread.sleep(1);
                            i++;
                            slideMenuPane.setBounds(slideMenuPane.getX(), slideMenuPane.getY(), i, slideMenuPane.getHeight());
                        }
                        for(int i=110;i<=210;i=i+35){
                            Thread.sleep(1);
                            i++;
                            slideMenuPane.setBounds(slideMenuPane.getX(), slideMenuPane.getY(), i, slideMenuPane.getHeight());
                        }
                        for(int i=210;i<=260;i=i+5){
                            Thread.sleep(1);
                            i++;
                            slideMenuPane.setBounds(slideMenuPane.getX(), slideMenuPane.getY(), i, slideMenuPane.getHeight());
                        }
                    }catch(Exception e){
                        System.out.println(e);      
                    }
                }
            };
            th.start();
        }
    }

where menuToggle (contained in Other Components ) is just used to know if the menu bar is already opened. In effect this something like this:

点击按钮之前 点击按钮后

However, when I click the menu icon button, there is a blink before the animation starts, it shows the menu bar with its width to be 60px and it lasts for a millisecond. Is there anyway I can remove the blink?

Thread.Sleep() could be causing the issue. Using a Swing Timer to check how much time has elapsed since it last ran shouldn't pause the thread or create graphical issues like Thread.Sleep() does.

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