JAVA第五次作业

本文介绍了一个简单的Java程序,用于列出指定文件夹中的所有文件及子目录,并通过Swing实现了基本的GUI界面。程序使用了JFrame和JComboBox等组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

老师,没有弄懂,就参考同学


import java.awt.Color;
import java.awt.BorderLayout;
import java.io.File;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;




import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public class FileUtils extends JFrame{
    /**
     * 列出指定文件夹(目录)中的所有文件或目录的名额
     * @param dri File类型指定的文件夹(目录)
     * @throws IllegalAccessException 
     */
      public static String listDirectory(File dir) throws IllegalAccessException{
          if(!dir.exists()){
        throw new IllegalAccessException("目录"+dir+"不存在");
          }
          
          //判断dir下关联的文件和目录是否存在
          if(!dir.isDirectory()){
              //如果不存在抛出异常
              throw new IllegalAccessException(dir + "不是目录");
          }
          String[] fileName = dir.list();
          String name ="";
          for(String a : fileName){
              name=name+a+"\n";}
           return name;
          }
         
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         // TODO Auto-generated method stub
        JFrame frame=new JFrame();
        JPanel main_panel =new JPanel(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setLayout(null);
        String[] itme =new String[]{".jpg",".zip",".doc"};
        JComboBox box=new JComboBox();
        for(int i=0;i<itme.length;i++){
            box.addItem(itme[i]);
        }
        box.setEnabled(true);
        box.setEditable(true);
        box.setMaximumRowCount(6);
        box.setBounds(230,30,130,25);
     
         
        frame.setBounds(400,300,400,200);
        frame.setVisible(true);
        JTextArea main_text =new JTextArea();
        main_text.setBackground(Color.BLACK);
        JScrollPane AA=new JScrollPane();
        AA.setViewportView(main_text);
        main_text.setEnabled(false);
        main_panel.add(box,BorderLayout.NORTH);
        main_panel.add(AA,BorderLayout.CENTER);
        frame.add(main_panel);
         
        
        
        try {
            String str=FileUtils.listDirectory(new File("C:\\Users\\Administrator\\Desktop\\Java作业5\\soundPlayer\\audio"));
            main_text.setText(str);
        }
        catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    frame.setBounds(300,400,300,400);
    frame.setVisible(true);
    
    }

}

  

转载于:https://www.cnblogs.com/blogwy/p/5398894.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值