鉴于最近在项目中遇到JFileChooser的问题,特在此做个总结
1.关于其中显示某种格式类型的文件需要用到的过滤器,下面贴出部分的关键代码做参考
public class FileUtil extends javax.swing.filechooser.FileFilter{
public boolean accept(java.io.File f) {
if (f.isDirectory())
return true;
return f.getName().endsWith(".dcm"); //设置为选择以.dcm为后缀的文件
}
public String getDescription(){
return ".dcm";
}
public static void main(String[] args){
FileUtil fileFilter=new FileUtil();
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter(fileFilter);
jfc.setFileSelectionMode(JFileChooser.APPROVE_OPTION);
jfc.setSize(500, 400);
jfc.setLocation(w, h);
jfc.showDialog(new JLabel(), "选择");
File file = jfc.getSelectedFile();
String filepath = file.getAbsolutePath();
if (filepath != null && filepath.length() !=0 &&filepath.endsWith(".dcm")) {
// System.out.println("打开的路径是"+filepath);
mainFrame.renewImagePanel(2,filepath);
}else{
new MessageDialog("请选择dcm格式的图片!");
logger.info("尚未选择图片");
}
}
}
2.关于UIManager的样式问题
根据本机系统的样式来决定当前的样式感官
try {
String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(lookAndFeel);
} catch (Exception e) {
e.printStackTrace();
}