Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 731|回复: 0

求救啦,帮忙看看哪里错了~~点击生成的程序的按钮没反应

[复制链接]

该用户从未签到

发表于 2011-11-3 08:19:29 | 显示全部楼层 |阅读模式
Java学习者论坛
本人新手~~谢啦
点擊连接等按钮都没反应?不懂
/*
* 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();
}
}

附图~


欢迎来到Java学习者论坛,转载请注明地址:http://www.javaxxz.com.
回复

使用道具 举报

该用户从未签到

发表于 2011-11-3 08:19:41 | 显示全部楼层
没有事件监听器
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-5-5 00:39 , Processed in 0.459554 second(s), 46 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表