將指定目錄下的全部文件顯示到列表框(JList)組件中,以下圖所示:
java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;對象
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;blog
public class FileUtils {io
/**event
System.out.println();
//使用listFiles()方法實現只列出目錄下的全部文件名稱
File[] files = dir.listFiles();
for(File file:files){
//若是是文件,則輸出
if(file.isFile()){
System.out.println(file);
}
}class
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);import
a.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stubList
try {
FileUtils.listDirectory(new File("D:\soundPlayer"));
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}file
}方法
}