GUI-文本框列出指定目录内容

通过读取文本框内容、搜索对应目录的内容、并输出在文本区当中。

 

import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class PracticeUsed  {

    public static void main(String[] args)   {
        new myWindow();
        
        
        
    }
}

class myWindow
{
    private Frame f;
    private Button but;
    private TextField tf;
    private TextArea ta;
    
    myWindow()
    {
        init();
    }
    public void init()
    {
        f = new Frame("我的窗口");
        f.setBounds(300,200,600,500);
        f.setLayout(new FlowLayout());
        ta = new TextArea(25,60);
        tf = new TextField(40);
        f.add(tf);
        but = new Button("我的按钮");
        f.add(but);
        f.add(ta);
        
        myEvent();
        
        f.setVisible(true);
        
    }
    
    private void myEvent()
    {
        but.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        String dirPath = tf.getText();
                        File f = new File(dirPath);
                        if(f.exists()&&f.isDirectory())
                        {
                            ta.setText("");
                            String[] names = f.list();
                            for( String name : names)
                            {
                                ta.append(name+"\r\n");
                            }
                            tf.setText("");
                        }
                        
                        
                    }
                });
        f.addWindowListener(new WindowAdapter()
                {
                    public void windowClosing(WindowEvent e)
                    {
                        System.out.println("我关了");
                        System.exit(0);
                    }
                });
        
        
    }
    
    
    
}

 

 

posted @ 2019-11-12 09:18  蚂蚁雅黑1010  阅读(135)  评论(0编辑  收藏  举报