黑色星球
风遇浪,海市蜃楼

列目录来练习文本框和对话框

  1 package dfhg;
  2 
  3 import java.awt.*;
  4 import java.awt.event.*;
  5 import java.io.*;
  6 class  MyWindowDemo
  7 {
  8     private Frame f;
  9     private TextField tf;
 10     private Button but;
 11     private TextArea ta;    
 12     private Dialog d;
 13     private Label lab;
 14     private Button okBut;
 15 
 16     MyWindowDemo()
 17     {
 18         init();
 19     }
 20 //窗口内容
 21     public void init()
 22     {
 23         f = new Frame("my window");
 24         f.setBounds(300,100,600,500);
 25         f.setLayout(new FlowLayout());
 26 
 27         tf = new TextField(60);
 28         but = new Button("转到");
 29         ta = new TextArea(25,70);
 30 
 31 //建对话框
 32         d = new Dialog(f,"提示信息-self",true);//true diag不操作,frame不能 用
 33         d.setBounds(400,200,240,150);
 34         d.setLayout(new FlowLayout());
 35         lab = new Label();
 36         okBut = new Button("确定");
 37 
 38         
 39         
 40         d.add(lab);
 41         d.add(okBut);
 42         f.add(tf);
 43         f.add(but);
 44         f.add(ta);
 45 
 46         myEvent();
 47         
 48         f.setVisible(true);
 49     }
 50     //事件
 51     private void  myEvent()
 52     {
 53 //对话框事件
 54         okBut.addActionListener(new ActionListener()
 55         {
 56             public void actionPerformed(ActionEvent e)
 57             {
 58                 d.setVisible(false);
 59             }
 60         });
 61         d.addWindowListener(new WindowAdapter()
 62         {
 63             public void windowClosing(WindowEvent e)
 64             {
 65                 d.setVisible(false);
 66             }
 67         });
 68 //文本事件
 69         tf.addKeyListener(new KeyAdapter()
 70         {
 71             public void keyPressed(KeyEvent e)
 72             {
 73                 if(e.getKeyCode()==KeyEvent.VK_ENTER)
 74                     showDir();
 75             }
 76         });
 77 
 78 //button事件
 79         but.addActionListener(new ActionListener()
 80         {
 81             public void actionPerformed(ActionEvent e)
 82             {
 83                 showDir();
 84                 
 85             }
 86         });
 87 //窗口关闭
 88         f.addWindowListener(new WindowAdapter()
 89         {
 90             public void windowClosing(WindowEvent e)
 91             {
 92                 System.exit(0);    
 93             }
 94         });
 95     }
 96 //列表  用文件遍历
 97     private void showDir()
 98     {
 99         String dirPath = tf.getText();
100                 
101         File dir = new File(dirPath);
102 
103         if(dir.exists() && dir.isDirectory())
104         {
105             ta.setText("");
106             String[] names = dir.list();
107             for(String name : names)
108             {
109                 ta.append(name+"\r\n");//TextArea的apend
110             }
111         }
112         //输入错误
113         else
114         {
115             String info = "您输入的信息:"+dirPath+"是错误的。请重输";
116             lab.setText(info);
117             d.setVisible(true);
118         }
119     }
120 
121     public static void main(String[] args) 
122     {
123         new MyWindowDemo();
124     }
125 }

 

posted on 2017-03-24 15:04  黑色星球  阅读(199)  评论(0编辑  收藏  举报