简体   繁体   English

Java Swing图像幻灯片显示为什么图片不变

[英]Java Swing Image Slide Show why a picture don't change

Hi I create a class ImageSlide2 and I have thread and only one time a picture change why ? 嗨,我创建了一个ImageSlide2类,并且只有线程,并且一次更改图片,为什么?

I don't know why only time a picture change. 我不知道为什么只一次改变图片。 The slide show have to all time display a changed picture This is my code : 幻灯片必须一直显示更改的图片,这是我的代码:

public class ImageSlide2 extends JLabel {

    private Timer tm;
    private int xx = 0;
    String[] list = {
        "C:/Users/022/workspace22/EkranLCD/res/images/1.png", //0
        "C:/Users/022/workspace22/EkranLCD/res/images/3.png" //1     
    };

    public ImageSlide2(int x, int y, int width, int height) {
        setBounds(x, y, width, height);
        //Call The Function SetImageSize
        SetImageSize(list.length - 1);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        new Thread(new Runnable() {
            public void run() {
                try {
                    System.out.println(xx);
                    SetImageSize(xx);
                    xx += 1;
                    if (xx >= list.length) {
                        xx = 0;
                    }
                } catch (Exception ie) {
                }
            }
        }).start();

    }
    //create a function to resize the image 

    public void SetImageSize(int i) {

        ImageIcon icon = new ImageIcon(list[i]);
        Image img = icon.getImage();
        Image newImg = img.getScaledInstance(Config.xSize / 2, Config.ySize / 2, Image.SCALE_SMOOTH);
        ImageIcon newImc = new ImageIcon(newImg);
        setIcon(newImc);
    }
}

Try to change your method by using javax.swing.Timer. 尝试使用javax.swing.Timer更改您的方法。 Something like this: 像这样:

public ImageSlide2(int x, int y, int width, int height) {
    setBounds(x, y, width, height);
    //Call The Function SetImageSize
    SetImageSize(list.length - 1);

    final Timer t = new Timer(1000, new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            try {
                System.out.println(xx);
                SetImageSize(xx);
                xx += 1;
                if (xx >= list.length) {
                    xx = 0;
                }
            } catch (Exception ie) {
            }
        }
    });
    // t.setRepeats(false); // when you want to execute it at once
    t.start();
}

A solution based on thread is the following - you need to add the sleep in a loop that continuously paints and then sleeps etc: 以下是基于线程的解决方案-您需要将睡眠添加到一个连续绘制的循环中,然后进行睡眠等:

public ImageSlide2(int x, int y, int width, int height) {
    setBounds(x, y, width, height);
    //Call The Function SetImageSize
    SetImageSize(list.length - 1);


    new Thread(new Runnable() {
        public void run() {
            while(true)
            try {
                System.out.println(xx);
                SetImageSize(xx);
                xx += 1;
                if (xx >= list.length) {
                    xx = 0;
                }
                Thread.sleep(1000);
            } catch (Exception ie) { ie.printStackTrace();
            }

        }
    }).start();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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