读书人

跪求大神指定为啥点击按钮不好使

发布时间: 2013-08-06 16:47:25 作者: rapoo

跪求大神指定,为什么点击按钮不好使啊
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) );



}
}
String 类 Box
[解决办法]

引用:
Quote: 引用:

我晕。原来你不知道啊
我还以为你故意使用这种用法的呢。。

//点击yes按钮
yes.addActionListener(new ActionListener() {

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

这个我知道 我的意思是如果我有多个按键 引起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);

读书人网 >J2SE开发

热点推荐