我在工具栏中有一个按钮,它必须保存我在Java中的JFrame中绘制的内容。它可以工作,但它目前充当“另存为”按钮。我试图在保存文件后覆盖文件而不显示对话框。有人可以帮我修复它吗?保存一个文件并在Java GUI中存在时覆盖它
我的代码:
JFileChooser fileChooser2;
this.fileChooser2 = new JFileChooser();
fileChooser2.addChoosableFileFilter(new TxtFilter2());
编辑
public class TxtFilter2 extends FileFilter
{
public boolean accept(java.io.File file)
{
if (file.isDirectory())
return true;
return (file.getName().endsWith("xml"));
}
public String getDescription()
{
return "Save (*.xml)";
}
}
这是按钮本身与行动:
if (ev.getActionCommand()=="Save2")
{
fileChooser2.setDialogType(JFileChooser.SAVE_DIALOG);
fileChooser2.setDialogTitle("Save as XML file format");
res=this.fileChooser2.showSaveDialog(this);
if (res==JFileChooser.APPROVE_OPTION)
{
this.net.saveToFile(fileChooser2.getSelectedFile().getPath()+".xml");
}
}
ev.getActionCommand()==“Save2” –
'fileChooser2.showSaveDialog(this)'打开一个对话框。如果你不想这样做,那么实现一些逻辑,当第一次保存文件位置时存储文件的位置,如果存储位置,则保存到文件位置。 – Poohl
您在FileFilter中显示JFileChooser对话框?这将造成严重破坏。 FileFilter的工作是根据是否显示文件来返回true或false。它不应该做任何事情。 – VGR