Swing中的图像幻灯片

时间:2014-01-28 09:06:40

标签: java swing slideshow

我必须在幻灯片中查看一些图像.. 为此我已经加载了一些图像并编写了这段代码。 在代码中我正在创建一个框架并添加一个标签,我在其中设置标签的图像图标。 但是当我运行我的程序时,框架会挂起。

这是我的代码吼叫。, 任何人都可以帮忙..!

 public class Slideshow extends SwingWorker<Integer, Integer> {

        File outputFolder;
        ScreenVO screenVO;
        ImageHelper helper;

        public Slideshow(File outputFolder, ScreenVO screenVO) {
            this.outputFolder = outputFolder;
            this.screenVO = screenVO;
            this.helper = new ImageHelper();
        }

        public void start(final File outputFolder, ScreenVO screenVO) {
            try {

                final JFrame frame = new JFrame();
                final JPanel panel = new JPanel();

                frame.add(panel);
                frame.setVisible(true);
                frame.setSize(new Dimension(200, 200));
                frame.addKeyListener(new KeyListener() {

                    @Override
                    public void keyTyped(java.awt.event.KeyEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void keyReleased(java.awt.event.KeyEvent e) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void keyPressed(java.awt.event.KeyEvent e) {
                        if (e.getKeyCode() == e.VK_ESCAPE) {
                            frame.dispose();
                        }

                    }
                });

                //screenVO.getScreen().setFullScreenWindow(frame);

                /* frame.setExtendedState(JFrame.MAXIMIZED_BOTH); */

                        File[] listOfFile = PhotoliciousUtils
                                .filterJPEGImagesFromFolder(outputFolder
                                        .listFiles());
                        List list = PhotoliciousUtils.nameOfFiles(listOfFile);
                        while (!Thread.interrupted()) {
                            File[] listOfFiles = PhotoliciousUtils
                                    .filterJPEGImagesFromFolder(outputFolder
                                            .listFiles());

                            if (list.size() > listOfFiles.length) {
                                list.clear();
                                list = PhotoliciousUtils.nameOfFiles(listOfFiles);
                            } else {
                                for (final File file : listOfFiles) {
                                    try {
                                        panel.removeAll();
                                        JLabel fullImage = new JLabel(helper
                                                .createThumbnails(file));
                                        panel.add(fullImage);
                                        Thread.sleep(5000);

                                    } catch (Exception ex) {
                                        ex.printStackTrace();
                                    }
                                }
                            }
                        }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public static ScreenVO[] fetchListOfScreen() {
            ScreenVO[] listOfScreenVO = new ScreenVO[10];
            try {
                GraphicsDevice[] listOfScreen;
                GraphicsEnvironment ge = GraphicsEnvironment
                        .getLocalGraphicsEnvironment();
                listOfScreen = ge.getScreenDevices();
                for (int i = 0; i < listOfScreen.length; i++) {
                    ScreenVO screenVO = new ScreenVO();
                    if (i == 0) {
                        screenVO.setName("Primary Screen" + " : "
                                + listOfScreen[i].getDisplayMode().getWidth() + "x"
                                + listOfScreen[i].getDisplayMode().getHeight());
                    } else {
                        screenVO.setName("Screen" + i + 1 + " : "
                                + listOfScreen[i].getDisplayMode().getWidth() + "x"
                                + listOfScreen[i].getDisplayMode().getHeight());
                    }
                    screenVO.setScreen(listOfScreen[i]);
                    listOfScreenVO[i] = screenVO;
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
            return listOfScreenVO;
        }

        @Override
        protected Integer doInBackground() throws Exception {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    start(outputFolder, screenVO);
                }
            });
            return null;
        }

    }

2 个答案:

答案 0 :(得分:3)

您在invokeLater()中呼叫doInBackground()。这显然将代码放回到事件调度线程 - 正是您试图通过使用SwingWorker避免的。如果加载代码不是特别耗时,您只需使用摆动Timer就可以逃脱。否则,继续使用SwingWorker(或自定义线程)进行加载,并仅在EDT中访问swing组件。

此外,虽然需要在EDT中创建组件,但您应该尝试将其与幻灯片代码明确区分开来。它不属于后台任务。创建窗口和内容(用invokeLater包装),然后有一个不同的方法来更改图片(计时器方法是最简单的,所以我从那开始)。

答案 1 :(得分:1)

创建一段时间后会改变图像的线程

Thread t=new Thread(new Runnable(){
   public void run(){
       try{
           Thead.sleep(1000);
           //write code here to set image to jLabel
       }catch(Exception e){
       }
   }
});
t.start()