qq_40140043
qq_40140043
2019-04-23 11:09

【求助】新手学习Java GUI遇到一些问题,希望大神帮忙解答一下,运行完Eclipse报了一大堆错误

  • java
  • eclipse
/*需求:做一个窗体
 * 功能是递归查找某目录下所有的MP4文件
 * 
 *  
 */
package Test;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class MyWindowDemo2 {
    private Frame f;
    private Button but;
    private TextField tf;
    private TextArea ta;

    MyWindowDemo2(){
        init();
    }

    public void init(){
        f = new Frame("查找mp4");

        f.setBounds(600,300,600,500);
        f.setLayout(new FlowLayout());

        but = new Button("查找");

        tf = new TextField(60);

        ta = new TextArea(25,70);

        f.add(tf);
        f.add(but);
        f.add(ta);

        myEvent();

        f.setVisible(true);
    }

    private void myEvent(){
        f.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        });

        but.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String dirPath = tf.getText();

                ta.setText("");

                listMp4File(dirPath);
            }
        });
    }

    public void listMp4File(String dirPath){
        File dir = new File(dirPath);

        if(dir.isDirectory()){
            File[] files = dir.listFiles();
            for(File file : files){
                if(file.isDirectory()){
                    listMp4File(file.getPath());
                }
                else{
                    if(file.getName().endsWith("mp4")){
                        ta.append(file.getName());
                    }
                }
            }
        }
        else{
            System.out.println("目录不存在!");
        }
    }

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

![图片说明](https://img-ask.csdn.net/upload/201904/23/1555989048_548222.png)


  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答