0
我正在使用Netbeans开发编辑器应用程序。我写了代码保存文件使用Jfilechooser保存功能正常工作。 但我在编写简单保存按钮的代码时遇到了问题。当前打开的选项卡的 。 给我一些想法来保存当前标签的内容,例如ctrl + s功能。 这里是我的代码保存为方法。 在此先感谢。保存文件并另存为文件方法
private void saveAsmActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
int returnVal = fileChooser.showSaveDialog(this);
if (returnVal == fileChooser.APPROVE_OPTION)
{
File dir1 = fileChooser.getCurrentDirectory();
String dir = dir1.getPath();
String name = fileChooser.getSelectedFile().getName();
//if it dont have .txt at end of name then add it
if (!name.endsWith(".txt"))
{
name = (name + ".txt");
}
try{
File file = new File(dir,name);
int res = 0;
if(file.exists())
{
res = JOptionPane.showConfirmDialog(null, "This file already exists, Overwrite it?");
}
if(res == 0)
{
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
FilePanel selectedComp = (FilePanel)tabbedPane.getSelectedComponent();
if (selectedComp != null) {
String text = selectedComp.getTextArea().getText();
bw.write(text);
} else {
System.out.println("No component selected");
}
bw.close();
tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(), name);
ClosableTab.apply(tabbedPane, tabbedPane.getTabCount()-1);
}
}
catch(Exception e)
{
System.out.println("err");
}
}
}
你愿意给我一些代码示例 –
你有你需要的所有代码。在“另存为”方法中,将文件作为FilePanel上的字段存储,然后在“保存”中检索文件并写入该文件。除了向FilePanel添加一个File域并将您的IO从“另存为”复制并粘贴到“保存”之外,您并不需要做任何事情。 – Radiodef
我已经尝试过,但我无法在重点选项卡中检索正确的文件地址。 我尝试了很多,但无法解决地址问题。 请帮我写保存事件。 –