简体   繁体   中英

jLabel Icon Animation Glictching

I'm trying to make the person walk but for some reason when I switch icons the label returns to it's default location for a split second then updates to the where I set the bounds. Extremely annoying, any help is much appreciated. Thank You.

public class Main extends JFrame implements ActionListener{

JLabel x = new JLabel("");

ImageIcon player1 = new ImageIcon("C:\\Users\\Kyle\\Documents\\NetBeansProjects\\Testing52\\src\\testing52\\Player1.png");
ImageIcon player2 = new ImageIcon("C:\\Users\\Kyle\\Documents\\NetBeansProjects\\Testing52\\src\\testing52\\Player2.png");

static int count;

Timer timer;

Main(){

    timer = new Timer(100,this);

    setVisible(true);
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setFocusable(false);
    add(x);

    timer.start();
}

public static void main(String [] args){

    Main main = new Main();    
}

@Override
public void actionPerformed(ActionEvent e) {

    count += 1;

    if(count == 10){
        x.setIcon(player1);
    }
    if(count == 20){
        x.setIcon(player2);
    }

    if(count == 30){           
        count = 0;   
    }

    x.setBounds(0, 0,60,60);
}
}

The setBounds(...) call only works when the layout used is null . Having said that, don't use null layouts (see this ), but instead use proper layout managers and component positioning.

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