首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请教内联类应该如何设置变量

2012-01-30 
请问内联类应该怎么设置变量先看程序public String GetFileChooser(String strExt,String strButton,boole

请问内联类应该怎么设置变量
先看程序
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变量出错,虽然知道错误的地方,但不知道该如何更正望指教

[解决办法]

Java code
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;    } } 

热点排行
Bad Request.