how to auto change image in java swing?

前端 未结 2 1062
灰色年华
灰色年华 2020-12-12 05:42

Hi i am creating a java desktop application where i want to show image and i want that all image should change in every 5 sec automatically i do not know how to do this

2条回答
  •  醉话见心
    2020-12-12 06:14

    one way of achieving your goal is setting a swing.Timer to notify its action listeners every 5 seconds, set your class to be the listener for the timer and implement the actionListener interface by having an actionPerformed method which will change all images using their setImage method. the code should look like this:

    public class ImageGallery extends JFrame implements ActionListener {
    Timer timer;
    
    public ImageGallery() {
        timer = new Timer(5000, this);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == timer) {
            for (int i=0; i

    }

提交回复
热议问题