【问题标题】:Slideshow of images in SwingSwing 中的图像幻灯片
【发布时间】:2014-01-28 09:06:39
【问题描述】:

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

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

 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;
        }

    }

【问题讨论】:

  • catch (Exception e) { ..形式的代码修改为catch (Exception e) { e.printStackTrace(); // very informative! ..

标签: java swing slideshow


【解决方案1】:

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

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

【讨论】:

  • 您是否建议将代码放在invokeLater() 中以放入Slideshow() 的构造函数中?
  • @ItachiUchiha 我将大致以这种方式重新排序代码:一个主类,我基本上只使用invokeLater() 来调用 UI 的创建。然后在 UI 完成后启动幻灯片放映的计时器。 (或者是一个摇摆工人,但只有当应用程序感觉这样响应更快时才需要这样做)。
【解决方案2】:

创建线程会在一段时间后更改图像

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()

【讨论】:

    猜你喜欢
    • 2017-12-31
    • 2011-08-23
    • 1970-01-01
    • 2021-02-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多