如何“打开”和“保存”使用java

发布于 2024-09-16 06:22:50 字数 221 浏览 6 评论 0原文

我想用java制作一个“打开”和“保存”对话框。我想要的示例如下图所示:

打开:

打开文件对话框

保存:

保存文件对话框

我该如何执行此操作?

I want to make an "Open" and "Save" dialog in java. An example of what I want is in the images below:

Open:

Open file dialog

Save:

Save file dialog

How would I go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

赢得她心 2024-09-23 06:22:50

您想要使用 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

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.

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:

http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html

对你再特殊 2024-09-23 06:22:50

我建议查看 javax.swing.JFileChooser

这是一个网站,其中包含一些用作“打开”和“保存”的示例。 http://www.java2s.com/Code/Java/Swing-JFC /DemonstrationofFiledialogboxes.htm

这比自己实现要少得多。

I would suggest looking into 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

This will be much less work than implementing for yourself.

秋凉 2024-09-23 06:22:50

也许你可以看看 JFileChooser ,它允许您在一行代码中使用本机对话框。

Maybe you could take a look at JFileChooser, which allow you to use native dialogs in one line of code.

夜雨飘雪 2024-09-23 06:22:50

您可以在 Java 教程中找到文件对话框的介绍 。 Java2s 还有一些示例代码

You can find an introduction to file dialogs in the Java Tutorials. Java2s also has some example code.

只想待在家 2024-09-23 06:22:50

首先,您需要阅读 Oracle 的教程来了解如何执行基本操作Java 中的 /O。

之后,您将需要查看有关如何使用文件选择器

First off, you'll want to go through Oracle's tutorial to learn how to do basic I/O in Java.

After that, you will want to look at the tutorial on how to use a file chooser.

岁吢 2024-09-23 06:22:50

您可能还想考虑使用 SWT(另一个 Java GUI 库)的可能性。每种方法的优缺点列于:

Java 桌面应用程序:SWT 与 SWT摇摆

You may also want to consider the possibility of using SWT (another Java GUI library). Pros and cons of each are listed at:

Java Desktop application: SWT vs. Swing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文