2015-09-04 132 views
0

我想要做的是一个简单的程序,将文件保存在用户指定的某个位置。我想在文件菜单中使用保存选项中的ActionListener来执行此操作。我不确定JPopupMenu会对此有用。有任何想法吗?如何显示弹出窗口

+1

我“觉得”你想'JFileChooser',请参见[如何使用文件挑肥拣瘦(https://docs.oracle.com/javase/tutorial/ uiswing/components/filechooser.html) – MadProgrammer

+0

我正在尝试,但我没有看到保存选项,只是打开一个 –

+1

'int returnVal = fc.showSaveDialog(FileChooserDemo.this);' – MadProgrammer

回答

2

看看FileChooserDemo.java

相关部分是这样的:

int returnVal = fc.showSaveDialog(FileChooserDemo.this); 
if (returnVal == JFileChooser.APPROVE_OPTION) { 
    File file = fc.getSelectedFile(); 
    //This is where a real application would save the file. 
    log.append("Saving: " + file.getName() + "." + newline); 
} else { 
    log.append("Save command cancelled by user." + newline); 
} 
+0

谢谢!这正是我想要做的! –

+0

当我保存文件时,保存的文本全部排成一行,我可以更改吗? –

+0

什么文字?这里没有文字。您只需获取一个File对象并写入即可。 – Burkhard