请问内联类应该怎么设置变量
先看程序
public String GetFileChooser(String strExt,String strButton,boolean bSave)
{
JFileChooser jf = new JFileChooser(".");
jf.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jf.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
String filePath = f.getAbsoluteFile().toString().toUpperCase();
if (filePath.endsWith(strExt))
return true;
else
return false;
}
public String getDescription()
{
return strExt;
}
});
jf.setApproveButtonText(strButton);
int result = -1;
if(bSave)
result = jf.showSaveDialog(null);
else
result = jf.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION)
return jf.getSelectedFile().getPath();
else
return null;
}
程序指明是strExt变量出错,虽然知道错误的地方,但不知道该如何更正望指教
[解决办法]
public String GetFileChooser(String strExt,String strButton,boolean bSave) { final String str = strExt;//加上这句.. 内部类要引用外部类的变量..该变量必须是final的. JFileChooser jf = new JFileChooser("."); jf.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); jf.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { String filePath = f.getAbsoluteFile().toString().toUpperCase(); if (filePath.endsWith(str)) return true; else return false; } public String getDescription() { return str; } }); jf.setApproveButtonText(strButton); int result = -1; if (bSave) result = jf.showSaveDialog(null); else result = jf.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) return jf.getSelectedFile().getPath(); else return null; } }