这个例程的目的是将用户输入的用户名和密码和用户的登陆当前时间写到d:\\userlog.txt文件中
但是执行之后没有任何反应,感觉好象是按钮事件没有起作用,望高手指教,谢谢!!
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
/**
 * @author Administrator
 *
 */
public class ATM extends JApplet implements ActionListener{
    JLabel weclome,code,password;
    JPanel p1;
    JTextField textcode,textpassword;
    JButton ok,exit;
    GridLayout gb;
    GridBagConstraints gbc;
    Date date;
    GregorianCalendar gcal;
    public void init(){
     p1=new JPanel();
     p1=(JPanel)getContentPane();
     gb=new GridLayout(2,3);
     p1.setLayout(gb);
          JLabel weclome=new JLabel("欢迎使用ATM综合服务模拟系统");
     JLabel code=new JLabel("用户名");
     JLabel password=new JLabel("密码");
     JTextField textcode=new JTextField(16);
     JTextField textpassword=new JTextField(16);
     JButton ok=new JButton("登陆");
     JButton exit=new JButton("退出");
     
         
     
    
     p1.add(code);
     p1.add(textcode);
     p1.add(ok);
     p1.add(password);
     p1.add(textpassword);
     p1.add(exit);
     ok.addActionListener(this);
     exit.addActionListener(this);
     
     
    }
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
JButton source=(JButton)event.getSource();
if(source==ok){
date=new Date();
gcal=new GregorianCalendar();
gcal.setTime(date);
String entry="user:"+textcode.getText()+",password:"+textpassword.getText()+
"login in:"+gcal.get(Calendar.HOUR)+":"+gcal.get(Calendar.MINUTE)+":"+
"DATE:"+gcal.get(Calendar.MONTH)+"/"+gcal.get(Calendar.DATE)+"/"+
                        gcal.get(Calendar.YEAR);
try{

FileOutputStream foutputstream=new FileOutputStream("d:\\userlog.txt");


for(int i=0;i<entry.length();i++)
foutputstream.write(entry.charAt(i));
foutputstream.close();

System.out.println("已成功写入日志");
}catch(IOException e){
showStatus("不能写入日志"+e);
}
}
if(source==exit){
getAppletContext().showStatus("你点的是取消");
return;
}
}
}

解决方案 »

  1.   

    你尝试一下把实现ActionListener这个接口的部分放到内部类里面实现。使用一个eventHandle的方法,把按钮的触发写在里面像这样:ok.addActionListener(this),exit.addActionListener(this)。用FileOutputStream写出的文件会有乱码的,你还不如用FileWriter来写,移植性高些。
      

  2.   

    在你的代码里面使用了ok.addActionListener(this),this代表的是哪个呢.applet还是ActionListener.
      

  3.   

    谢谢你的回复,问题找到了,是因为我重复定了两次ok和exit!!