Swing JFileChooser设置位置,大小,图标,打开上次打开文件。
记一次Swing开发的 JfileChooser
的使用。再次印证了一个常识:stackoverflow
真就YYDS。当国内的网站不能解决开发遇到的问题时候,考虑去stackoverflow或者Github的项目地址的 issure
中找找答案。
设置位置,图标
setLocation()
和 setBounds()
没有如愿。按照stackoverflow
中的提示:继承JFileChoose
创建自己的文件选择器 myJFileChoose
。
public class myJFileChooser extends JFileChooser{
public myJFileChooser(){} // 别忘加上这个构造器
public myJFileChooser(String currentDirectoryPath){
super(currentDirectoryPath, (FileSystemView) null);
}
@Override
protected JDialog createDialog(Component parent)
throws HeadlessException {
JDialog dlg = super.createDialog(parent);
dlg.setLocation(660, 80); // 设置位置
dlg.setIconImage(Toolkit.getDefaultToolkit().getImage(OrginFrame.class.getResource("/images/company2.png"))); // 设置JfileChoose的图标Icon
return dlg;
}
}
设置大小
JFileChooser fileChooser = new JFileChooser();
fileChooser.setPreferredSize( new Dimension(100, 100)
设置上次打开位置
if(DesDir.getText().length()!=0){ // 如果上次的文本输入框不为空,从该处创造文件选择器。
fc.setCurrentDirectory(new File(DesDir.getText()));
}else{
fc.setCurrentDirectory(new File("C:\\"));
}
另外,Swing桌面程序用exe4j
和inno
的打包成能在电脑端运行的程序太大了,主要是依赖jar包和Java环境都能占 90%(实测:项目5M,生成jar
包之后25M,打包好java
环境之后之后91M)。因此:如果你想做一个在任何人电脑上都能运行的Java桌面应用,一定要想好是否真的能接受这么大的的包,不能接受趁早换实现方案。隔壁 Python Qt
C#
可能比这个小的多。