【问题标题】:How to prepopulate save dialog box using JFileChooser?如何使用 JFileChooser 预填充保存对话框?
【发布时间】:2015-07-14 03:28:04
【问题描述】:
我正在制作一个将滤镜应用于图片的应用程序。它会在选择源文件后立即询问用户将新文件保存在哪里。是否可以用源文件名+_filtername预填充保存对话框?
【问题讨论】:
-
如果您查看 JFileChooser docs.oracle.com/javase/7/docs/api/javax/swing/… 的在线 Java 文档,您将看到一个名为 setSelectedFile 的函数。查看在线 java 文档是找到答案的好方法(比使用 StackOverflow 快得多)。在 JFileChooser 上搜索还会显示其他页面,这些页面可能会告诉您答案(尽管我没有阅读它们来找出答案)。如果您有任何问题,请打开一个新问题并发布代码和更具体的问题。不过,您可能想删除这个。
标签:
java
swing
jfilechooser
【解决方案2】:
这个打开对话框的例子
JFileChooser fd = new JFileChooser();
fd.showOpenDialog(this);
if(fd.getSelectedFile()==null&&pro_pic_text.getText().toString().trim().equals(""))
{
JOptionPane.showMessageDialog(this,"Choose your Profile Picrure","Warning",JOptionPane.WARNING_MESSAGE);
}
else
{
fileName = fd.getSelectedFile().getAbsolutePath();
File ff = new File(fileName);
String ffname = ff.getName();
int aa = ffname.indexOf(".");
fftype = ffname.substring(aa+1);
if(fftype.equals("png") || fftype.equals("PNG") || fftype.equals("JPEG") || fftype.equals("jpeg") || fftype.equals("JPG") || fftype.equals("jpg"))
{
if(ff.length()<=51300)
{
pro_pic_text.setText(fileName);
}
else
{
JOptionPane.showMessageDialog(this,"File size larger then 50kb not allowed","Warning",JOptionPane.WARNING_MESSAGE);
pro_pic_text.setText("");
}
}
else
{
JOptionPane.showMessageDialog(this,"Choose JPEG, JPG or PNG File","Warning",JOptionPane.WARNING_MESSAGE);
pro_pic_text.setText("");
}
}
使用这个代码我的英语不好
此代码有一些验证,例如支持的文件必须小于 50KB,并且只有 JPEG、jpeg、jpg、JPG、PNG、png 文件
我觉得这对你有帮助