标签:
我们这节课练习的作业是从硬盘中扫描指定位置的文件再将其显示到图形用户界面中。在本节课的作业中主要运用了两部分的知识,一部分是GUI界面,我添加了一个列表框,一个下拉菜单,还有一个组合框,但是利用组合框进行分类的功能没有实现。另一部分的知识就是利用流方面的知识获取硬盘上的文件。以下是我的运行效果图。
package test.com;
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 FileUtils {
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);
String[] likes ={".mp3",".mp4",".jpg",".txt"};
JComboBox combox =new JComboBox(likes);
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));
contentPane.add(combox, BorderLayout.NORTH);
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout(8, 8));
JLabel label = new JLabel("File lists");
label.setFont(new Font("Serif",Font.PLAIN,16));
fileList.setForeground(Color.BLACK);
fileList.setBackground(Color.green);
fileList.setSelectionBackground(new Color(87,49,134));
fileList.setSelectionForeground(new Color(140,171,226));
JScrollPane scrollPane = new JScrollPane(fileList);
scrollPane.setColumnHeaderView(label);
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 {
FileUtils.listenDirectory(new File("D:\\")) ;
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
标签:
原文地址:http://www.cnblogs.com/WangJie0108/p/5395064.html