import java.awt.BorderLayout;
导入 java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
导入 java.io.File;
import javax.swing.JFileChooser;
导入 javax.swing.JFrame;
import javax.swing.JMenu;
导入 javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class SaveDialog {
public static void main(String[] args) {
final JFrame window = new JFrame();
window.setSize(200,200);
window.setLocationRelativeTo(null);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
JMenuBar jmb = new JMenuBar();
JMenu jm = new JMenu(Ficher”);
JMenuItem Save = new JMenuItem(保存”);
jm.add(保存);
jmb.add(jm);
window.add(jmb,BorderLayout.NORTH);
JFileChooser fc = new JFileChooser();
Save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法 stub
int val_retour = fc.showSaveDialog(window);
if (val_retour == JFileChooser.APPROVE_OPTION) {
文件文件 = fc.getSelectedFile();
//显示文件的绝对路径
System.out.println(绝对路径:”+file.getAbsolutePath()+\n”);
} else {
System.out.println(注册已取消\n”);
}
}
});
}
}
绝对路径: C:\Users\VAIO\Documents\CC++\test.txt您可以更改文件选择模式。例如,通过以下代码行,可以select JFileChooser:
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);另一种选择模式是 FILES_AND_DIRECTORIES跨度>。默认设置为 FILES_ONLY.
Please disable your ad blocker and refresh the window to use this website.