王爱沉 2015-12-25 14:06 采纳率: 4.3%
浏览 1920

java在自定义事件监听器是遇到的问题

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.awt.BorderLayout.*;
public class FileSearcher
{
public static void findFiles(String baseDirName, String targetFileName, List fileList)
{
String tempName=null;
File baseDir=new File(baseDirName);
if(!baseDir.exists()||!baseDir.isDirectory())
{
System.out.println("文件查找失败"+baseDirName+"不是一个目录");
}
else
{
String[] filelist=baseDir.list();
for(int i=0;i {
File readfile=new File(baseDirName+"\\"+filelist[i]);
if(!readfile.isDirectory())
{
tempName=readfile.getName();
if(FileSearcher.wildcardMatch(targetFileName, tempName))
{
fileList.add(readfile.getAbsoluteFile());
}
}
else if(readfile.isDirectory())
{
findFiles(baseDirName+"\\"+filelist[i],targetFileName,fileList);
}
}
}
}
private static boolean wildcardMatch(String pattern ,String str)
{
int patternLength=pattern.length();
int strLength=str.length();
int strIndex=0;
char ch;
for(int patternIndex=0;patternIndex {
ch=pattern.charAt(patternIndex);
if(ch=='*')
{
while(strIndex {
if(wildcardMatch(pattern.substring(patternIndex + 1),str.substring(strIndex)))
{
return true;
}
strIndex++;
}
}
else if(ch=='?')
{
strIndex++;
if(strIndex>strLength)
{
return false;
}
}
else
{
if(strIndex>=strLength||(ch!=str.charAt(strIndex)))
{
return false;
}
strIndex++;
}
}
return (strIndex==strLength);
}
public void init()
{
JFrame jf=new JFrame();
JPanel jp=new JPanel();
TextArea jta=new TextArea(20,30);
JTextField jtf=new JTextField(10);
JTextField jtf1=new JTextField(10);
JButton jb=new JButton("搜索");
jp.add(jtf);
jp.add(jtf1);
jp.add(jb);
jf.setLayout(new BorderLayout());
jf.add(jta,SOUTH);
jf.add(jp,NORTH);
jf.pack();
jf.setVisible(true);
myListener Listener=new myListener(this);
jtf1.addActionListener(Listener);
jb.addActionListener(Listener);
}

public static void main(String[] args)
{

    new FileSearcher().init();

}

}
class myListener implements ActionListener
{

public void actionPerformed(ActionEvent e)
        {
            jta.setText("");
            String baseDIR=jtf.getText();
            String fileName=jtf1.getText();
            List resultList=new ArrayList();
            FileSearcher.findFiles(baseDIR,fileName,resultList);
            if(resultList.size()==0)
            {
               System.out.println("worng");
            }
            else
            {
                for(int i=0;i<resultList.size();i++)
                {

                    jta.append(resultList.get(i).toString()+"\r\n");
                    System.out.println(resultList.get(i));

                }
            }
        }

}
错误提示
---------- 编译java ----------
FileSearcher.java:97: 错误: 无法将类 myListener中的构造器 myListener应用到给定类型;
myListener Listener=new myListener(this);
^
需要: 没有参数
找到: FileSearcher
原因: 实际参数列表和形式参数列表长度不同
FileSearcher.java:114: 错误: 找不到符号
jta.setText("");
^
符号: 变量 jta
位置: 类 myListener
FileSearcher.java:115: 错误: 找不到符号
String baseDIR=jtf.getText();
^
符号: 变量 jtf
位置: 类 myListener
FileSearcher.java:116: 错误: 找不到符号
String fileName=jtf1.getText();
^
符号: 变量 jtf1
位置: 类 myListener
FileSearcher.java:128: 错误: 找不到符号
jta.append(resultList.get(i).toString()+"\r\n");
^
符号: 变量 jta
位置: 类 myListener
注: FileSearcher.java使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
5 个错误

输出完成 (耗时 1 秒) - 正常终止

  • 写回答

2条回答 默认 最新

  • threenewbee 2015-12-25 14:21
    关注

    错误太多了,你基本的java语法都不会
    TextArea jta=new TextArea(20,30);,这个jta定义在一个函数中是局部变量,在另一个函数怎么访问

    myListener Listener=new myListener(this);
    你myListener没有带有一个参数的构造函数,怎么传的 this

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿