可视化弹出选择框,选择文件
返回未string
public String chooseSavedFile() {
int result = 0;
File file = null;
String path = null;
JFileChooser fileChooser = new JFileChooser();
FileSystemView fsv = FileSystemView.getFileSystemView(); //注意了,这里重要的一句
System.out.println(fsv.getHomeDirectory()); //得到桌面路径
fileChooser.setCurrentDirectory(fsv.getHomeDirectory());
fileChooser.setDialogTitle("请选择要保存的文件...");
fileChooser.setApproveButtonText("确定");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
JFrame chatFrame = new JFrame();
result = fileChooser.showOpenDialog(chatFrame);
if (JFileChooser.APPROVE_OPTION == result) {
path=fileChooser.getSelectedFile().getPath();
if(path.endsWith(".json")) {
System.out.println("path: "+path);
return path;
}
else {
JOptionPane.showMessageDialog(null, "请选择json文件","ERROR",JOptionPane.ERROR_MESSAGE);
}
}
return "";
}
本文介绍了如何使用Java Swing实现一个可视化弹出选择框,引导用户从桌面路径选择并确保文件为.json格式,适用于文件管理与数据导入任务。
458

被折叠的 条评论
为什么被折叠?



