我想在FileDialog中实现文件过滤功能,代码如下:
class Myfilter implements FilenameFilter//用于文件过滤
{
public boolean accept(File dir,String fname)
{
return fname.toLowerCase().endsWith(".txt");
}
} public void jButton_actionPerformed(ActionEvent e)//单击按钮Button打开文件对话框
{
FileDialog fdlg=new FileDialog(this,"打开", FileDialog.LOAD);
Myfilter filter=new Myfilter();
fdlg.setFilenameFilter(filter);
fdlg.setVisible(true);
}但是却不能实现我想要的功能,打开文件对话框后,过滤条中的信息仍然是(*.*),哪位大哥能帮帮小妹呢?
class Myfilter implements FilenameFilter//用于文件过滤
{
public boolean accept(File dir,String fname)
{
return fname.toLowerCase().endsWith(".txt");
}
} public void jButton_actionPerformed(ActionEvent e)//单击按钮Button打开文件对话框
{
FileDialog fdlg=new FileDialog(this,"打开", FileDialog.LOAD);
Myfilter filter=new Myfilter();
fdlg.setFilenameFilter(filter);
fdlg.setVisible(true);
}但是却不能实现我想要的功能,打开文件对话框后,过滤条中的信息仍然是(*.*),哪位大哥能帮帮小妹呢?
楼主也可以考虑用JFileChooser
JFileChooser不是用来选择文件的吗
fdlg.setFileName("*.txt");
不过只能过虑文件的类型不能具体到某个文件
public void setFilenameFilter(FilenameFilter filter)Sets the filename filter for this file dialog window to the specified filter. Filename filters do not function in Sun's reference implementation for Windows 95, 98, or NT 4.0.
你的机器是什么系统的?我是2003试了也不行,才看到这个。