Huu*_*ece 48 java swing jfilechooser
我想在java中创建一个"打开"和"保存"对话框.我想要的一个例子如下图所示:
打开:
保存:
我该怎么做呢?
Eri*_*son 60
你想使用一个JFileChooser
对象.它将打开并是模态,并在打开它的线程中阻塞,直到您选择文件.
打开:
JFileChooser fileChooser = new JFileChooser(); if (fileChooser.showOpenDialog(modalToComponent) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); // load from file }
保存:
JFileChooser fileChooser = new JFileChooser(); if (fileChooser.showSaveDialog(modalToComponent) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); // save to file }
您可以设置更多选项来设置文件扩展名筛选器或当前目录.有关javax.swing.JFileChooser
详细信息,请参阅API .Oracle网站上还有一个"如何使用文件选择器"的页面:
http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
Mik*_*ark 37
我建议调查一下 javax.swing.JFileChooser
这是一个网站,其中包含一些使用"打开"和"保存"的示例. http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm
这比为自己实施要少得多.