使用Swing&Netbeans GUI编辑器保存文件/打开文件对话框

我是Java的初学者。 我正在使用GUI编辑器在netbeans 7(.3)IDE中创建一个简单的文本编辑器。 我面临的主要问题是我无法保存/打开文件。 我创建了“保存”按钮。 当我删除文件选择器时,它作为一个普通的打开文件对话框嵌入在java窗口中,根本没有任何function。 我还尝试在单击保存按钮时(在“源”视图中)创建新的jFileChooser,但它不起作用。

简而言之,我需要一个简单的打开/保存对话框。 按下“保存”按钮后,将打开保存对话框,并将文件保存在用户选择的任何名称和.rtf或.txt扩展名的任何位置。 (PS:是否可以在Java中以.docx或.doc保存文件?)
当按下“Open”btn时,它会打开.rtf或.txt中的文件(再次,是否可以通过文件选择器在Java中打开.docx或.doc?)。

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser saveFile = new JFileChooser(); if saveFile.showSaveDialog(modalToComponent) == JFileChooser.APPROVE_OPTION { File xyz = saveFile.getSelectedFile(); } } 

代码在这里: https : //docs.google.com/file/d/0B766zz1iJ1LRN2lGRjNtM29vN2M/edit?usp=sharing

我创建了一个示例UI,显示了保存和打开文件对话框。 单击“保存”按钮打开“保存”对话框,然后单击“打开”按钮打开文件对话框。

 import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; public class FileChooserEx { public static void main(String[] args) { Runnable r = new Runnable() { @Override public void run() { new FileChooserEx().createUI(); } }; EventQueue.invokeLater(r); } private void createUI() { JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); JButton saveBtn = new JButton("Save"); JButton openBtn = new JButton("Open"); saveBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { JFileChooser saveFile = new JFileChooser(); saveFile.showSaveDialog(null); } }); openBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { JFileChooser openFile = new JFileChooser(); openFile.showOpenDialog(null); } }); frame.add(new JLabel("File Chooser"), BorderLayout.NORTH); frame.add(saveBtn, BorderLayout.CENTER); frame.add(openBtn, BorderLayout.SOUTH); frame.setTitle("File Chooser"); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } 

我认为你面临三个问题:

  1. 了解FileChooser
  2. 写/读文件
  3. 了解扩展和文件格式

广告1.您确定已将FileChooser连接到正确的面板/容器吗? 我会在这个问题上找一个简单的教程,看看它是否有效。 这是最好的学习方式 – 通过向前迈出小而足够大的步伐。 将问题分解为这些部分有时可能很棘手;)

广告。 2.保存或打开文件后,您应该有写入或读取文件的方法。 在这个问题上还有很好的例子,很容易理解主题。

广告。 3.具有扩展名和文件格式的文件之间存在差异。 您可以将任何文件的格式更改为您想要的任何文件,但这不会影响其内容。 它可能只是使与此类扩展相关联的应用程序的文件不可读。 TXT文件很简单 – 你阅读你写的东西。 XLS,DOCX等需要更多的工作,通常框架是解决这些问题的最佳方法。

以任何格式保存是非常有可能的。 检查以下内容 – http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

2,你究竟期望保存对话框能够正常工作,就像那样工作,打开一个doc文件是非常有可能的 – http://srikanthtechnologies.com/blog/openworddoc.html

这是一个例子

 private void doOpenFile() { int result = myFileChooser.showOpenDialog(this); if (result == JFileChooser.APPROVE_OPTION) { Path path = myFileChooser.getSelectedFile().toPath(); try { String contentString = ""; for (String s : Files.readAllLines(path, StandardCharsets.UTF_8)) { contentString += s; } jText.setText(contentString); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void doSaveFile() { int result = myFileChooser.showSaveDialog(this); if (result == JFileChooser.APPROVE_OPTION) { // We'll be making a mytmp.txt file, write in there, then move it to // the selected // file. This takes care of clearing that file, should there be // content in it. File targetFile = myFileChooser.getSelectedFile(); try { if (!targetFile.exists()) { targetFile.createNewFile(); } FileWriter fw = new FileWriter(targetFile); fw.write(jText.getText()); fw.close(); } catch (IOException e) { e.printStackTrace(); } } }