zoukankan      html  css  js  c++  java
  • java第五次作业

    import javax.swing.*;
    import java.awt.*;
    import java.io.File;
    
    public class work extends JFrame{
    
        /**
         * @param args
         */
        
        public static String listDirectory(File dir) throws IllegalAccessException{
            if(!dir.exists()){
                throw new IllegalAccessException("目录"+dir+"不存在");
            }
            if(!dir.isDirectory()){//判断是不是目录
                throw new IllegalArgumentException(dir+"不是目录");
            }
            String[] fileName = dir.list();    
            String name ="";
            for(String a : fileName){
                name=name+a+"
    ";}
            return name;
            }
        
        public static void main(String[] args) {
            // 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[]{"","2"};
            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=work.listDirectory(new File("D:\soundPlayer"));
                main_text.setText(str);
            }
            catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
    
            
            frame.setBounds(400,200,400,200);
            frame.setVisible(true);
            
        
            
            
    
        
        }
    }

    将指定路径下的文件显示到文本框中,程序还添加了组合框以及背景色。通过本次作业,我熟练的掌握了从磁盘中读取文件的操作。

  • 相关阅读:
    Maven 安装 / 常用配置 / 阿里maven中央仓库
    记录:框架
    Mysql配置文件详解 my.cof
    Vmware虚拟机设置静态IP地址
    虚拟机安装centos发现inet为127.0.0.1,导致Xshell连接不上
    Servlet小案例总结
    Win10 提升系统响应速度
    从顶端插入,滚动展示(Demo):
    eclipse +cygwin+C++
    大型网站技术学习-3. 容器Docker与kubernetes
  • 原文地址:https://www.cnblogs.com/wangweiwei666/p/5396633.html
Copyright © 2011-2022 走看看