import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Toolkit; import java.io.File; 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.border.EmptyBorder; public class file{
public static void listenDirectory(File dir) throws IllegalAccessException{
if(!dir.exists()){
throw new IllegalAccessException("目录"+dir+"不存在。"); }
if(!dir.isDirectory()){ throw new IllegalArgumentException(dir+"不是目录"); }
String[] fileNames = dir.list(); JList fileList = new JList(fileNames);
JFrame frm = new JFrame(); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(6, 10, 10, 10)); contentPane.setLayout(new BorderLayout(5, 5));
JPanel pane = new JPanel(); pane.setLayout(new BorderLayout(8, 8));
fileList.setForeground(Color.blue); fileList.setBackground(Color.BLACK); fileList.setSelectionBackground(Color.gray); fileList.setSelectionForeground(Color.orange);
JScrollPane scrollPane = new JScrollPane(fileList); scrollPane.setColumnHeaderView(new JLabel("File lists"));
pane.add(scrollPane, BorderLayout.CENTER); contentPane.add(pane, BorderLayout.CENTER);
frm.add(contentPane);
frm.setBounds(500,300,300,400); frm.setVisible(true); } public static void main(String[] args) {
try { file.listenDirectory(new File("E:\\")) ; } catch (IllegalAccessException e) {
e.printStackTrace(); } } }