第五次作业

import java.awt.;
import java.awt.event.
;

import javax.swing.;
import javax.swing.event.
;
import javax.swing.border.*;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import java.io.*;

/**

  • A simple sound player. To start, create an instance of this class.

  • The sound player can play sound clips in WAV, AU and AIFF formats

  • with standard sample rates.

  • @author Michael Kolling and David J Barnes

  • @version 1.0
    */
    public class SoundPlayerGUI extends JFrame
    implements ChangeListener, ActionListener
    {

    private final String AUDIO_DIR = "audio";

    private JList fileList;

    /**

    • Main method for starting the player from a command line.
      */
      public static void main(String[] args)
      {
      SoundPlayerGUI gui = new SoundPlayerGUI();
      }

    /**

    • Create a SoundPlayer and display its GUI on screen.
      */
      public SoundPlayerGUI()
      {
      super("SoundPlayer");

      String[] audioFileNames = findFiles(AUDIO_DIR, null);
      makeFrame(audioFileNames);
      }

    /**

    • Play the sound file currently selected in the file list. If there is no
    • selection in the list, or if the selected file is not a sound file,
    • do nothing.
      */

    private String[] findFiles(String dirName, String suffix)
    {
    File dir = new File(dirName);
    if(dir.isDirectory()) {
    String[] allFiles = dir.list();
    if(suffix == null) {
    return allFiles;
    }
    else {
    List selected = new ArrayList();
    for(String filename : allFiles) {
    if(filename.endsWith(suffix)) {
    selected.add(filename);
    }
    }
    return selected.toArray(new String[selected.size()]);
    }
    }
    else {
    System.out.println("Error: " + dirName + " must be a directory");
    return null;
    }
    }

    // ------- ChangeListener interface (for Slider) -------

    /**

    • ChangeListener method for slider changes. This method is called
    • when the slider value is changed by the user.
    • @param evt The event details.
      */

    private void makeFrame(String[] audioFiles)
    {
    // the following makes sure that our application exits when
    // the user closes its window

     JPanel contentPane = (JPanel)getContentPane();
    
    
     
     // Specify the layout manager with nice spacing
     contentPane.setLayout(new BorderLayout(8, 8));
    
     // Create the left side with combobox and scroll list
     JPanel leftPane = new JPanel();
     {
         leftPane.setLayout(new BorderLayout(8, 8));
    
      
    
         // Create the combo box.
       
         
    
         // Create the scrolled list for file names
         fileList = new JList(audioFiles);
         fileList.setForeground(new Color(140,171,226));
         fileList.setBackground(new Color(1,0,0));
         fileList.setSelectionBackground(new Color(87,49,134));
         fileList.setSelectionForeground(new Color(140,171,226));
         JScrollPane scrollPane = new JScrollPane(fileList);
         scrollPane.setColumnHeaderView(new JLabel("Audio files"));
         leftPane.add(scrollPane, BorderLayout.CENTER);
     }
     contentPane.add(leftPane, BorderLayout.CENTER);
    
     // Create the center with image, text label, and slider
    
     pack();
     
     // place this frame at the center of the screen and show
     Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
     setLocation(d.width/2 - getWidth()/2, d.height/2 - getHeight()/2);
     setVisible(true);
    

    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

    }

    @Override
    public void stateChanged(ChangeEvent arg0) {
    // TODO Auto-generated method stub

    }

    }

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class FileUtils {

/**

  • 列出指定目录下的所有文件和子目录的名称
  • @throws IllegalAccessException

*/
public static void listDirectory(File dir) throws IllegalAccessException{
//判断File对象dir是否存在,使用File类的exists()方法
if(!dir.exists()){
//如不存在,抛出异常
throw new IllegalAccessException("目录" + dir +"不存在");
}
//判断dir是否是目录,使用File类的isDirectory()方法
if(!dir.isDirectory()){
//如果dir不是目录,抛出异常
throw new IllegalArgumentException(dir + "不是目录");
}
//获得指定目录dir下的文件及子目录并输出(1.list();2.listFiles())
//使用list()方法实现
String[] fileName = dir.list();
for(String s :fileName){
System.out.println(dir + ""+ s);
}

System.out.println();
//使用listFiles()方法实现只列出目录下的所有文件名称
File[] files = dir.listFiles();
for(File file:files){
//如果是文件,则输出
if(file.isFile()){
System.out.println(file);
}
}

JFrame a =new JFrame("Text");
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.setLayout(new BorderLayout());
String[] AA ={".txt",".ppt",".jpg",".docx"};
JComboBox l1=new JComboBox(AA);
a.add(l1,BorderLayout.NORTH);
JList list = new JList(files);
list.setForeground(new Color(140,200,220));
list.setSelectionBackground(new Color(100,80,150));
list.setSelectionForeground(new Color(140,200,220));
JScrollPane sp = new JScrollPane(list);
//
a.add(sp,BorderLayout.CENTER);
a.setBounds(400,300,400,300);

a.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub

try {
FileUtils.listDirectory(new File("E:"));
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

原文地址:https://www.cnblogs.com/yinxinlei/p/5401851.html