gpt4 book ai didi

java - Swing 中的图像幻灯片

转载 作者:行者123 更新时间:2023-12-01 18:37:19 25 4
gpt4 key购买 nike

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

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

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

}

最佳答案

您正在 doInBackground() 中调用 invokeLater()。这会显式地将代码放回事件调度线程 - 这正是您使用 SwingWorker 试图避免的情况。如果加载代码不是特别耗时,您可以简单地使用 swing Timer 来摆脱困境。 。否则,继续使用 SwingWorker(或自定义线程)进行加载,并且仅在 EDT 中访问 swing 组件。

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

关于java - Swing 中的图像幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21401149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com