您的位置:程序门 -> java -> j2se / 基础类



求助啦~~为什么点击其中的按钮都没反应~~


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求助啦~~为什么点击其中的按钮都没反应~~
发表于:2007-12-26 11:56:53 楼主
/*
  *   main.java
  *
  *   created   on   2007年12月19日,   下午8:35
  *
  *   to   change   this   template,   choose   tools   ¦   template   manager
  *   and   open   the   template   in   the   editor.
  */
import   sun.net.ftp.*;
import   sun.net.*;
import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;
import   java.util.*;
import   java.io.*;
public   class   useftp   extends   jframe   implements       actionlistener{

ftpclient   aftp;
dataoutputstream   outputs;                                       //数据输出流
telnetinputstream   ins;                                           //输入流
telnetoutputstream   outs;                                       //输出流
jtextarea   lsarea=new   jtextarea(40,100);
jlabel   lblprompt   =new   jlabel("没有连接主机",label.left);
jlabel   lblhost   =new   jlabel("主机名:");
jlabel   lbluid   =new   jlabel("用户名:");
jlabel   lblpwd   =new   jlabel("密码:");
jlabel   lblfile   =new   jlabel("要下载的文件名:");
jlabel   lbldir   =new   jlabel("存放文件的路径:");
jbutton   btnconn=new   jbutton("连接");
jbutton   btnclose=new   jbutton("断开");
jbutton   btndown=new   jbutton("下载");
jbutton   btnup=new   jbutton("上传");
jtextfield   txthost=new   jtextfield(34);
jtextfield   txtuid=new   jtextfield(14);
jpasswordfield   txtpwd=new   jpasswordfield(14);
jtextfield   txtdir=new   jtextfield(29);
jtextfield   txtfile=new   jtextfield(29);
int   ch;
string   a="没有连接主机";
string   hostname="";

public     useftp(){
        jpanel   p1=new   jpanel(new   flowlayout(flowlayout.left));
        jpanel   p2=new   jpanel(new   flowlayout(flowlayout.left));
        jpanel   p3=new   jpanel(new   flowlayout(flowlayout.left));
        btnclose.enable(false);
        p1.add(lblprompt);
        p2.add(lblhost);p2.add(txthost);p2.add(btnconn);
        p3.add(lbluid);p3.add(txtuid);p3.add(lblpwd);
        p3.add(txtpwd);p3.add(btnclose);
       
        jpanel   top   =new   jpanel(new   gridlayout(3,1));
        top.add(p1);top.add(p2);top.add(p3);
        lsarea.seteditable(false);
        jscrollpane   jsp=new   jscrollpane(lsarea);
        jpanel   bottom=new   jpanel(new   gridlayout(2,1));
        jpanel   p4=new   jpanel(new   flowlayout(flowlayout.left));
        jpanel   p5=new   jpanel(new   flowlayout(flowlayout.left));
        p4.add(lblfile);p4.add(txtfile);p4.add(btnup);
        p5.add(lbldir);p5.add(txtdir);p5.add(btndown);
        bottom.add(p4);bottom.add(p5);
        this.getcontentpane().add(top,"north");
        this.getcontentpane().add(jsp,"center");
        this.getcontentpane().add(bottom,"south");
        this.addwindowlistener(new   windowadapter(){
        public   void   windowclosing(windowevent   e){
        try{aftp.closeserver();
        }catch(exception   ee){}
        dispose();
        system.exit(0);
        }
        });
        setsize(520,400);
        settitle("ftp客户端");
        setvisible(true);
}
public   boolean   connect(string   hostname,string   uid,string   pwd){                                           //连接函数
this.hostname   =hostname;
lblprompt.settext("正在连接,请等待……");
try{
        aftp=new   ftpclient(hostname);
        aftp.login(uid,pwd);
        aftp.binary();
        showfilecontents();
}
catch(ftploginexception   e){
lblprompt.settext("无权限与主机:"+hostname+"连接!");
return   false;
}
catch(ioexception   e){
lblprompt.settext("连接主机:"+hostname+"失败!");
return   false;
}
catch(securityexception   e){
lblprompt.settext("无权限与主机:"+hostname+"连接!");
return   false;
}
lblprompt.settext("连接主机:"+hostname+"成功!");
return   true;
}
public   void   actionperformed(actionevent   e){                                                                         //点击按钮相应的响应
if(e.getsource()==btnconn){                                                                                                         //连接按钮
lblprompt.settext("正在连接,请等待……");
    if(connect(txthost.gettext(),txtuid.gettext(),txtpwd.gettext())){
    btnconn.setenabled(false);
    btnclose.setenabled(true);
    }
}
else   if(e.getsource()==btnclose){                                                                                             //断开按钮
try{
    aftp.closeserver();
    }
catch(ioexception   ee)   {}
btnconn.enable(true);
btnclose.enable(false);
lblprompt.settext("与主机"+hostname+"   连接已断开");
}
else   if(e.getsource()==btndown){                                                                                             //下载按钮
download(txtdir.gettext(),txtfile.gettext());
}
else   if(e.getsource()==btnup){                                                                                                 //上传按钮
    sendfile(txtdir.gettext()+"//"+txtfile.gettext());
}
}
public   void   sendfile(string   filepathname){                                                                         //上传文件;
        if   (aftp!=null){
        lblprompt.settext("正在粘贴文件,请耐心等待……");
        string   contentperline;
        try{
            string   fg=new   string("\\");
            int   index=filepathname.lastindexof(fg);
            string   filename=filepathname.substring(index+1);
            file   localfile=new   file(filepathname);
            randomaccessfile   file   =new   randomaccessfile(filepathname,"r");
            file.seek(0);
            outs   =aftp.put(filename);
            outputs=new   dataoutputstream(outs);
            while(file.getfilepointer() <file.length()){
            ch=file.read();
            outputs.write(ch);
          }
          outs.close();
          file.close();
            lblprompt.settext("粘贴成功!");
            showfilecontents();
            return;            
        }
        catch(ioexception   e){}
        }
        lblprompt.settext("粘贴失败!");
    }
    public   void   showfilecontents()   {                                                                       //显示目录和文件名
            stringbuffer   buf   =new   stringbuffer();
            lsarea.settext("");
            try{
                ins=aftp.list();                                                             //获得所有文件和目录的输入数据流;
                while((ch=ins.read())> =0){
                buf.append((char)ch);
                }
                lsarea.append(buf.tostring());
                ins.close();
            }catch(ioexception   e){}
    }
        public   void   download(string   dir,string   fname){                         //下载文件
        stringbuffer   buf   =new   stringbuffer();
        buf.setlength(0);
        try{
        file   f=new   file(new   file(dir),fname);       //通过路径和文件名够着一个文件
          randomaccessfile   file=new   randomaccessfile(f,"rw");
          ins=aftp.get(fname);                                 //得到所选文件的输入流
          while((ch=ins.read())> =0){                     //读取数据流
        buf.append((char)ch);
        }
        file.writebytes(buf.tostring());       //将缓冲区的数据以字符串形式写入文件
        file.close();                                                 //关闭文件
        lblprompt.settext("下载成功!");
        return;
        }     catch(exception   e){}
        lblprompt.settext("下载成功!");
      }
      public   static   void   main(string   args[]){
      font   font=new   font("jframe",font.plain,14);       //定义字体
      enumeration   keys=uimanager.getlookandfeeldefaults().keys();
      while(keys.hasmoreelements()){
      object   key=keys.nextelement();
      if(uimanager.get(key)instanceof   font)uimanager.put(key,font);
        }    
  new   useftp();
    }  
}
       
       
       


发表于:2007-12-26 15:29:341楼 得分:0
按钮并没有注册动作事件,例:btnconn.addactionllistener(this);
发表于:2007-12-26 16:10:372楼 得分:0
你不注册事件源它怎么知道要触发什么事件呢?比如:点击按钮,鼠标移动到按钮上,还有鼠标按下和弹起也会触发事件等等.
发表于:2007-12-26 16:25:463楼 得分:0
没有注册事件源,应该给每一个按钮都注册一个事件,如btnclose.addactionlistener(new   actionlistener()
{
      public   void   actionperformed(actionevent   event)
        {
…………………………………………………………处理过程
            }
}
)
发表于:2007-12-26 17:32:144楼 得分:0
没有监听   那些按钮就是可以响应鼠标的图片
发表于:2007-12-26 21:04:385楼 得分:0
监听能不能用个循环语句~~少些代码
发表于:2007-12-26 21:30:016楼 得分:0
学习


快速检索

最新资讯
热门点击