嗯,基本上我正在制作一个屏幕捕获程序,它使用 JFileChooser
来选择保存图像的位置。
问题是:当我选择使用 JFileChooser
保存时,它会使我的程序永远不会结束。我的意思是,如果我在保存后单击主 GUI 上的 x 按钮,它将消失但继续运行。
我尝试删除JFileChooser
并仅保存到默认位置,这解决了问题,然后我的程序正确关闭。
但是,我想使用JFileChooser
,但它使我的程序永远运行?
这是我的 JFileChooser 代码。
JFileChooser fc = new JFileChooser();
fc.removeChoosableFileFilter(fc.getFileFilter());
FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
fc.setFileFilter(filter);
if ( fc.showSaveDialog( new JFrame() ) == JFileChooser.APPROVE_OPTION ) {
String filepath = fc.getSelectedFile().getPath();
filepath += ".jpg";
File file = new File(filepath);
try {
ImageIO.write(capturedImage, "jpg", file);
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println(fc.getSelectedFile().getName() + ".jpg successfully saved.");
}
最佳答案
当您显示 JFileChooser
时,您正在创建一个新的 JFrame
fc.showSaveDialog(new JFrame())
这意味着,如果您的主 JFrame
设置为 DISPOSE_ON_CLOSE
,您的程序将不存在,因为您仍然拥有空框架。要解决此问题,请使用:
fc.showSaveDialog(null)
或
fc.showSaveDialog( yourMainJFrame )
您应该将主 JFrame 上的默认关闭操作设置为 EXIT_ON_CLOSE
例如:mainJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
关于java - JFileChooser 未处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28529093/