(1)首先使用Fire中的isDirectory方法提取文件中的内容,获得目录下的文件并输出。然后用JList方法输入到列表中。显示出来
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
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.JSlider;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main extends JFrame {
/**
* @param args
*/
static final String VERSION = "Version 1.0";
private static final String AUDIO_DIR = "audio";
private JList fileList;
private JSlider slider;
private JLabel infoLabel;
public Main()
{
super("name");
String[] FileNames = findFiles(AUDIO_DIR, null);
makeFrame(FileNames);
}
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<String> selected = new ArrayList<String>();
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;
}
}
private void makeFrame(String[] Files)
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel contentPane = (JPanel)getContentPane();
contentPane.setBorder(new EmptyBorder(0,0,0,0));
contentPane.setLayout(new BorderLayout(8,8));
JPanel Pane = new JPanel();
{
Pane.setLayout(new BorderLayout(4, 4));
fileList = new JList(Files);
fileList.setForeground(new Color(140,171,226));
fileList.setBackground(new Color(0,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("fileList"));
Pane.add(scrollPane, BorderLayout.CENTER);
}
contentPane.add(Pane, BorderLayout.CENTER);
pack();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(d.width/2 - getWidth()/2, d.height/2 - getHeight()/2);
setVisible(true);
}
public static void main(String[] args) {
Main gui = new Main();
}
}
(2)这个只是在了解代码的基础上稍作修改的,还不够完善。