如果没有匹配类型文件总是弹出很多窗口  我想只弹出一个,但是总是调不对,希望有高手可以帮忙解决,谢谢了
下面是程序
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.JFileChooser;public class MyWindows {

public static void main(String[] args) {
new MyFrame("网页木马监测");
}}
//窗口及实现其功能class MyFrame extends Frame implements ActionListener{
private MenuItem featureCode = new MenuItem("特征码库");
private MenuItem scanList = new MenuItem("扫描目录");
private MenuItem about = new MenuItem("关于");
private MenuItem setTime = new MenuItem("设定扫描时间");
String filePath = null;
File[] file2 = null;


MyFrame(String s){
super(s);
setLayout(null);
setBounds(220,100,800,600);
this.setBackground(Color.white);
this.setVisible(true);
this.addWindowListener(new MyWindowMonitor());         //窗口监听事件

//添加菜单   

 Menu choise = new Menu("选项");
 Menu set1 = new Menu("参数设定");
 choise.add(featureCode);
 set1.add(scanList);
 set1.addSeparator();
 set1.add(setTime);
 featureCode.setEnabled(true);
 MenuBar bar = new MenuBar();
 bar.add(choise);
 bar.add(set1);
 setMenuBar(bar);
 scanList.addActionListener(this);

}



//扫描目录菜单监听事件   -打开添加扫描目录对话框

public void actionPerformed(ActionEvent e){ 

if(e.getSource() == scanList){
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("浏览文件夹");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
chooser.showDialog(null, "确定");
try {
File fileSelect = chooser.getSelectedFile();
filePath = fileSelect.getPath();
       
//System.out.println(filePath);
File file = new File(filePath);
file2 = file.listFiles();
}catch(NullPointerException oe){
return;
}
       
        if(file2.length != 0) {
          for(int i=0; i<file2.length; i++) {
           System.out.println(file2[i].getName());                //需要注释掉  目录不空,调用扫描方法:
          
           scanMethod(file2[i]);
          }
        }else { //目录为空,弹出窗口
         final Dialog d1 = new Dialog(this,"");
         d1.setBounds(300, 300, 300, 100);
         d1.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
         d1.add(new Label("所选目录为空,请您重新选择目录O(∩_∩)O~"));
         d1.setVisible(true);
         d1.addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent e){
         d1.setVisible(false);
         }
         });
        }
       
        
}

}




//扫描方法:是文件夹还是文件,如果是文件,然后判断文件类型(网页),如果是,然后根据数据库扫描文件里内容

private void scanMethod(File file) {
String subName = null;
String fileText = null;
String fileTextF = null;
if(file.isFile()) {
        long nameL = file.getName().length();
          int subNameIndex = file.getName().lastIndexOf('.');                         //获取文件名字的后缀
          if(subNameIndex>0 && subNameIndex <nameL){
          subName = file.getName().substring(subNameIndex+1);
          }
if(!(subName.matches("htm")||subName.matches("html")||subName.matches("asp"))){        
final Dialog d2 = new Dialog(this,"");                    //如果文件夹中没有网页类型的文件就显示一个对话框提示
        d2.setBounds(300, 300, 300, 100);
        d2.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
        d2.add(new Label("当前目录及子目录没有匹配类型的文件~~"));
        d2.setVisible(true);
        d2.addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent e){
         d2.setVisible(false);
         }
        });  

}else {  //如果类型匹配就该匹配字符串了
try {
FileReader fd1 = new FileReader(file);
BufferedReader br1 = new BufferedReader(fd1);
while((fileText = br1.readLine())!=null){
fileTextF = fileText.replaceAll(" ","");
if(fileTextF.contains("hehehehe")){
JFrame f2 = new JFrame("");
Label trojanL = new Label("发现含有特征马的文件");
Label fp = new Label("文件路径为:"+ filePath +"文件名为:" + file.getName() );
f2.setLayout(new FlowLayout(FlowLayout.LEFT));
f2.add(trojanL);
f2.add(fp);
f2.setBounds(500, 300, 350, 300);
f2.setResizable(true);
f2.setVisible(true);
}
}


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}


System.out.println("okok");           //该扫描文件了
 
}
}else {
File[] childs = file.listFiles();
if(childs.length!=0){
for(int j=0;j<childs.length;j++) {
scanMethod(childs[j]);
}
}else{  
/*final Dialog d1 = new Dialog(this,"");
         d1.setBounds(300, 300, 300, 100);
         d1.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
         d1.add(new Label("所选目录为空,请您重新选择目录O(∩_∩)O~"));
         d1.setVisible(true);
         d1.addWindowListener(new WindowAdapter(){
         public void windowClosing(WindowEvent e){
         d1.setVisible(false);
         }
         });*/
}

}
}


 



}
// 窗口监听器 class MyWindowMonitor extends WindowAdapter{
public void windowClosing(WindowEvent e) {
   System.exit(0);
   }
}

解决方案 »

  1.   

    沒太細看
    有個建議
    你可以把這個方法scanMethod  改成返回boolean值
    把這段代碼d2.setBounds(300, 300, 300, 100);
    d2.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    d2.add(new Label("沒有匹配的"));
    d2.setVisible(true);
    d2.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    d2.setVisible(false);
    }
    });寫在外面,比如 scanMethod  返回false代表 沒有匹配  就顯示這個d2   你現在的寫法 是把這個d2放在循環里面 當然會出現多次了
      

  2.   


    for (int i = 0; i < file2.length; i++) {
    System.out.println(file2[i].getName()); // 需要注释掉
    // 目录不空,调用扫描方法: scanMethod(file2[i]);
    }
    你这里 每个文件都调用了一次scanMethod方法  每次都弹出一个对话框 所以有多少个文件就弹出了多少个对话框 把scanMethod方法写在其他地方应该就对了 你自己在改改吧
      

  3.   

    2楼的方法不错呵呵,你也可以在扫描前初始化一个ArrayList,每扫描到一个就把文件路径加到ArrayList中,调用完scanMethod(File file)后,查看ArrayList的size(),如果大于0说明有病毒文件则弹出提示框,如果为0说明没有病毒文件,提示"I'm clear"
      

  4.   

    恩,呵呵,昨天解决了,就是定义了个num ,嘿嘿,谢谢啦