import javax.swing.*; 
import java.awt.FlowLayout;
import java.awt.event.*;
import java.io.File;
import java.io.FilenameFilter;
class FileWindow extends JFrame implements ActionListener{ 
JTextField diretext,typetext;
JLabel direlabel,typelabel; 
JButton yes;
Box boxv,boxh1,boxh2;
public String dir,type;
File file;
public String[] filename;
FileWindow(String s){
setTitle(s);
diretext = new JTextField(10); 
diretext.addActionListener(this);
typetext = new JTextField(10);
typetext.addActionListener(this);
direlabel =new JLabel("请输入文件夹目录:");
typelabel =new JLabel("请输入文件类型: ");
JButton yes=new JButton("YES");
yes.addActionListener(this);
boxv=Box.createVerticalBox();
boxh1=Box.createHorizontalBox();
boxh2=Box.createHorizontalBox();
setLayout(new FlowLayout());
boxh1.add(direlabel);
boxh1.add(Box.createHorizontalStrut(8));
boxh1.add(diretext);
boxh2.add(typelabel);
boxh2.add(Box.createHorizontalStrut(17));
boxh2.add(typetext);
boxv.add(boxh1);
boxv.add(Box.createVerticalStrut(4));
boxv.add(boxh2);
boxv.add(Box.createVerticalStrut(4));
boxv.add(yes);
add(boxv);
setVisible(true);
setResizable(false);
setBounds(10,10,300,150);
validate();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==yes){
if(diretext.getText().equals("")==false&&typetext.getText().equals("")==false){
dir=diretext.getText().trim();
file=new File(dir);
type=typetext.getText().trim();
System.out.println(dir+":"+"*."+type);
filename=file.list(new FileAccept(type));
if(filename.length==0){
System.out.println("查无此文件");
}
else{
for(int i=0;i<filename.length;i++){
System.out.println(filename[i]);
}
}
}
}
else{
System.out.println("请重新输入");
}

}
}
class FileAccept implements FilenameFilter{
String str;
FileAccept(String s){
str="."+s;
}
public boolean accept(File dir,String name){
return name.endsWith(str);
}
}
public class FileWin { public static void main(String[] args) {
// TODO Auto-generated method stub
FileWindow filewindow=new FileWindow ("测试程序");
//String s1="D:\\";
//File file=new File();
//String[] filename=filewindow.file.list(new FileAccept(filewindow.type) );

}
}
StringBox

解决方案 »

  1.   

    ActionListenser呢?没有这个怎么让它知道你点了按钮。每一个按钮都要加一个这个。
      

  2.   

    加了啊  diretext.addActionListener(this);  typetext.addActionListener(this); yes.addActionListener(this)
      

  3.   

    public void actionPerformed(ActionEvent e){
     if(e.getSource()==yes){
        ...
     }
    }
    e.getSource()取得的是一个Jbutton对象吧!
      

  4.   

    e.getSource()==yes
    你这比较的是什么?
    这样,你输出
    System.out.println(e.getSource());System.out.println(yes);
    看效果。
      

  5.   

    我晕。原来你不知道啊
    我还以为你故意使用这种用法的呢。。//点击yes按钮
    yes.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    System.out.println("点击了yes按钮");
    }
    });
      

  6.   

    这个我知道 我的意思是如果我有多个按键 引起ActionEvent事件 我直接写一个actionperformed(){}用e.getSource()不能得到是哪个按键按下的吗?
      

  7.   

    有啊 actionperformed(){} 这个方法我写了啊
      

  8.   

    我按键后面加了(this)了啊 这个类又在后面实现了actionEvent接口了啊
      

  9.   

    这个我知道 我的意思是如果我有多个按键 引起ActionEvent事件 我直接写一个actionperformed(){}用e.getSource()不能得到是哪个按键按下的吗?
    楼主我在8楼的时候告诉你怎么做,你肯定没做是把?
    如果做了的话1分钟内就能找出你的错误。。
    System.out.println(e.getSource());
    System.out.println(yes);
    加上这两条输出语句,
    第一句输出:javax.swing.JButton[,113,52,56x28,alignmentX=0.0,alignmentY=0.5,border=javax.swing
    第二句输出:null
    知道哪里错了吧?
    direlabel =new JLabel("请输入文件夹目录:");
    typelabel =new JLabel("请输入文件类型: ");
    JButton yes=new JButton("YES");//这里改成yes=new JButton("YES");就OK了
    yes.addActionListener(this);
      

  10.   

    这个我知道 我的意思是如果我有多个按键 引起ActionEvent事件 我直接写一个actionperformed(){}用e.getSource()不能得到是哪个按键按下的吗?
    楼主我在8楼的时候告诉你怎么做,你肯定没做是把?
    如果做了的话1分钟内就能找出你的错误。。
    System.out.println(e.getSource());
    System.out.println(yes);
    加上这两条输出语句,
    第一句输出:javax.swing.JButton[,113,52,56x28,alignmentX=0.0,alignmentY=0.5,border=javax.swing
    第二句输出:null
    知道哪里错了吧?
    direlabel =new JLabel("请输入文件夹目录:");
    typelabel =new JLabel("请输入文件类型: ");
    JButton yes=new JButton("YES");//这里改成yes=new JButton("YES");就OK了
    yes.addActionListener(this);
    谢谢了 我做了 但是没看出来 经你提醒明白了 多谢了 这是说明我定义的那个成员变量yes没被分配实体 后面只是等于定义了一个局部变量(重新定义了一个按钮yes) 所以后面引用yes的时候显示的是null是吗?