黑马程序员技术交流社区

标题: 事件监听机制 [打印本页]

作者: LLLLL    时间: 2015-9-5 11:15
标题: 事件监听机制
哪里出错了 ?  求大神帮忙。。。

import java.awt.*;
import java.awt.event.*;
import java.io.*;
class  MyWindowDemo
{
         private Frame f;
         private Button b;
         private TextArea ta;
         private TextField tf;

         private Dialog d;
         private Label l;
         private Button ok;
         MyWindowDemo()
         {
                 info();
         }
         public void info()
         {
                 f=new Frame("my frame");
                 f.setBounds(300,100,600,500);
                 f.setLayout(new FlowLayout());
                 b=new Button("转到");
                 ta=new TextArea(25,70);
                 tf=new TextField(60);
                 f.add(tf);
                 f.add(b);
                 f.add(ta);
                 myEvent();
                 f.setVisible(true);
         }
         public void myEvent()
         {
                 ok.addActionListener(new ActionListener()
                 {
                         public void actionPerformed(ActionEvent e)
                         {
                                 d.setVisible(false);
                         }
                 });
                 d.addWindowListener(new WindowAdapter()
                 {
                         public void windowClosing(WindowEvent e)
                         {
                                 d.setVisible(false);
                         }
                 });
                 b.addActionListener(new ActionListener()
                 {
                         public void actionPerformed(ActionEvent e)
                         {
                                 String dirPath=tf.getText();
                                 File dir=new File(dirPath);
                                 if (dir.exists() && dir.isDirectory())
                                 {
                                         ta.setText("");
                                         String[] names=dir.list();
                                         for (String name : names)
                                         {
                                                 ta.append(name+"\r\n");
                                         }
                                 }
                                 else
                                 {
                                         d=new Dialog(f,"提示信息:",true);
                                         d.setBounds(400,200,240,150);
                                         d.setLayout(new FlowLayout());
                                         l=new Label();
                                         ok=new Button("确定");
                                         d.add(l);
                                         d.add(ok);
                                         
                                         String info="输入的信息:"+dirPath+"错误  重新输入";
                                         l.setText(info);
                                         d.setVisible(true);
                                 }
                                 //ta.setText(text);
                                 //System.out.println(text);
                                 //tf.setText("");
                         }
                 });
                 
                 f.addWindowListener(new WindowAdapter()
                 {
                         public void windowClosing(WindowEvent e)
                         {
                                 System.exit(0);
                         }
                 });
         }
         public static void main(String[] args)
         {
                 new MyWindowDemo();
         }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2