如何使用java“打开”和“保存”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3548140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to "Open" and "Save" using java
提问by Huuhaacece
I want to make an "Open" and "Save" dialog in java. An example of what I want is in the images below:
我想在 Java 中创建一个“打开”和“保存”对话框。我想要的一个例子如下图所示:
Open:
打开:
Save:
节省:
How would I go about doing this?
我该怎么做呢?
采纳答案by Mike Clark
I would suggest looking into javax.swing.JFileChooser
我建议调查 javax.swing.JFileChooser
Here is a site with some examples in using as both 'Open' and 'Save'. http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm
这是一个站点,其中包含一些用作“打开”和“保存”的示例。 http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm
This will be much less work than implementing for yourself.
这将比自己实施要少得多。
回答by Aaron Digulla
You can find an introduction to file dialogs in the Java Tutorials. Java2s also has some example code.
您可以在 Java 教程 中找到文件对话框的介绍。Java2s 也有一些示例代码。
回答by dbyrne
First off, you'll want to go through Oracle's tutorial to learn how to do basic I/O in Java.
首先,您需要阅读 Oracle 的教程以了解如何在 Java 中进行基本 I/O。
After that, you will want to look at the tutorial on how to use a file chooser.
之后,您将需要查看有关如何使用文件选择器的教程。
回答by Riduidel
Maybe you could take a look at JFileChooser, which allow you to use native dialogs in one line of code.
也许您可以看看JFileChooser,它允许您在一行代码中使用本机对话框。
回答by Erick Robertson
You want to use a JFileChooser
object. It will open and be modal, and block in the thread that opened it until you choose a file.
你想使用一个JFileChooser
对象。它将打开并且是模态的,并阻塞在打开它的线程中,直到您选择一个文件。
Open:
打开:
JFileChooser fileChooser = new JFileChooser(); if (fileChooser.showOpenDialog(modalToComponent) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); // load from file }
Save:
节省:
JFileChooser fileChooser = new JFileChooser(); if (fileChooser.showSaveDialog(modalToComponent) == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); // save to file }
There are more options you can set to set the file name extension filter, or the current directory. See the API for the javax.swing.JFileChooser
for details. There is also a page for "How to Use File Choosers" on Oracle's site:
您可以设置更多选项来设置文件扩展名过滤器或当前目录。有关javax.swing.JFileChooser
详细信息,请参阅 API 。Oracle 网站上还有一个“如何使用文件选择器”页面:
http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
回答by Sinjo
You may also want to consider the possibility of using SWT (another Java GUI library). Pros and cons of each are listed at:
您可能还想考虑使用 SWT(另一个 Java GUI 库)的可能性。各自的优缺点列于: