资源描述
渤海大学
信息科学与技术学院
计算机科学与技术
题目:目录&文件查看器
班 级: 2010级2班
学 号: 10060042
姓 名: 杨 朝 文
教 师: 沈 泽 刚
一 课程设计目的和要求。
1. 学院改革,注重实践与理论结合。
2. 熟用JDK虚拟平台和额eclipse 编程工具。
3. 自己勤动手写代码,熟悉Java语言。
4. 从中可以获取Java语言的完美结晶。
5. 培养脚踏实地的编程心态。
二 课程设计的内容。
1. 根据自己目前所学的知识,设计一个小小项目。
2. 运用类的继承extends 接口的实现 implements 。了解图形界面的的基本组件窗口
顶层容器 JFrame,布局管理器 BorderLayout,面板JPanel ,按钮JButton, 标签 JLabel,事件处理与常用组件 JLabel类,JButton类,JTextField类,JTextArea类,JScrollPane类,JOptionPane类,JMenuBar,JMenu,JMenuItem菜单设计。。。。。。
3. 运用了异常处理,java输入输出中有关目录操作的方法,表示时间,日期相关类和有关线程知识。
三 作品效果。
Java作品的源代码
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class DirectoryFile extends JFrame implements ActionListener{
JMenuBar jmb = new JMenuBar();
JMenu fileMenu = new JMenu("文件"),
BGMenu = new JMenu("背景"),
helpMenu = new JMenu("帮助");
JMenuItem jExit,jAbout,jRefresh,jShow,jThank,jFile,jBG0,jBG1,jBG2;
JTextField jtf = new JTextField(30);
JButton jbrowse = new JButton("浏览"),//浏览
jshow = new JButton("目录显示"),//显示
jfile = new JButton("文件显示");
JTextArea jta = new JTextArea();//内容显示
JScrollPane jsp = new JScrollPane(jta);
Icon img1 = new ImageIcon("4.jpg"),
img2 = new ImageIcon("5.jpg");
JLabel photos = new JLabel(img1),
jinput = new JLabel(":",img2,SwingConstants.CENTER),
shijian = new JLabel();
Thread th;
JPanel jp1 = new JPanel(),
jp2 = new JPanel();
String dname;
File fl;
String strTemp;
public DirectoryFile(){
setJMenuBar(jmb);
jmb.add(fileMenu);
jmb.add(BGMenu);
jmb.add(helpMenu);
jinput.setToolTipText("请输入查找的路径");
fileMenu.add(jShow = new JMenuItem("目录--"));
jShow.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
fileMenu.add(jFile = new JMenuItem("文件--"));
jFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
fileMenu.add(jRefresh = new JMenuItem("刷新--"));
jRefresh.setAccelerator(KeyStroke.getKeyStroke("F5"));
fileMenu.addSeparator();
fileMenu.add(jExit = new JMenuItem("退出--"));
jExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
BGMenu.add(jBG0 = new JMenuItem("默认--"));
jBG0.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1,InputEvent.CTRL_MASK));
BGMenu.addSeparator();
BGMenu.add(jBG1 = new JMenuItem("背景1--"));
jBG1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2,InputEvent.CTRL_MASK));
BGMenu.add(jBG2 = new JMenuItem("背景2--"));
jBG2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3,InputEvent.CTRL_MASK));
helpMenu.add(jAbout = new JMenuItem("关于--"));
jAbout.setAccelerator(KeyStroke.getKeyStroke("F11"));
helpMenu.add(jThank = new JMenuItem("感谢--"));
jThank.setAccelerator(KeyStroke.getKeyStroke("F12"));
jp1.add(jinput);
jtf.setEditable(true);//JTextField 可输入
jp1.add(jtf);
jp1.add(jbrowse);
jp1.add(jshow);
jp1.add(jfile);
dataDemo();
jp2.add(photos);
jtf.addActionListener(this);
jbrowse.addActionListener(this);
jshow.addActionListener(this);
jfile.addActionListener(this);
jBG0.addActionListener(this);
jBG1.addActionListener(this);
jBG2.addActionListener(this);
jAbout.addActionListener(this);
jThank.addActionListener(this);
jRefresh.addActionListener(this);
jExit.addActionListener(this);
jShow.addActionListener(this);
jFile.addActionListener(this);
this.add(jp1,BorderLayout.SOUTH);
this.add(jp2,BorderLayout.WEST);
this.add(jsp,BorderLayout.CENTER);
this.setSize(920,500);
this.setLocation(400,200);
setTitle("目录&文件查看器 作者:杨朝文");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jfile || e.getSource() == jFile){
showFile();
}
if(e.getSource() == jRefresh){
jta.setText("");
}
if(e.getSource() == jExit){
System.exit(0);
}
if(e.getSource() == jAbout){
JOptionPane.showMessageDialog(this,
"杨朝文保留@所有的权利!",
"关于Java",
JOptionPane.INFORMATION_MESSAGE );
}
if(e.getSource() == jThank){
JOptionPane.showMessageDialog(this,
"特别感谢 沈泽刚 老师!",
"关于Java",
JOptionPane.INFORMATION_MESSAGE );
}
if(e.getSource() == jBG0){
jta.setBackground(new Color(255,255,255));
}else if(e.getSource() == jBG1){
jta.setBackground(new Color(131,199,93));
}else if(e.getSource() == jBG2){
jta.setBackground(new Color(255,250,179));
}
if(e.getSource()==jshow || e.getSource() == jtf || e.getSource() == jShow){
if(!jtf.getText().equals("")){
jta.setText(showMessage(jtf.getText()));
}
else{
JOptionPane.showMessageDialog(null,"目录为空");
}
}
if(e.getSource()==jbrowse){
JFileChooser js=new JFileChooser();
js.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int num=js.showOpenDialog(this);
if(num==JFileChooser.APPROVE_OPTION){
jtf.setText(js.getCurrentDirectory().getAbsolutePath());
}
else{
}
}
}
public String showMessage(String path){ //显示目录方法
path=jtf.getText();
fl=new File(path);
if(fl.exists()){
File[] list=fl.listFiles();
Object[] data = new Object[8];
StringBuffer sb=new StringBuffer();
for(int i=0;i<list.length;i++){
if(list[i].isDirectory())
strTemp="目录";
else
strTemp="文件";
data[0] = list[i].getName();
data[1] = getTypeName(list[i].getName());
data[2] = String.valueOf(list[i].length()/1024/1024);
data[3] = String.valueOf((list[i].canRead()==true)?"可读"+strTemp:"不可读"+strTemp);
data[4] = String.valueOf((list[i].canWrite()==true)?"可修改"+strTemp:"不可修改"+strTemp);
data[5] = String.valueOf(new SimpleDateFormat().format(new Date(list[i].lastModified())));
data[6] = String.valueOf((list[i].isHidden()==true)?"隐藏"+strTemp:"非隐藏"+strTemp);
data[7]=String.valueOf((list[i].canExecute()==true)?"允许执行"+strTemp:"不允许执行"+strTemp);
if(list[i].isDirectory())
sb.append(data[1]+strTemp+"\t\t"+strTemp+"名:"+data[0]+"\t\t\t\t"+data[3]+"\t"+data[4]+"\t\t最后修改日期"+data[5]+"\t"+data[6]+"\t"+data[7]+"\n");
else
sb.append(data[1]+strTemp+"\t\t"+strTemp+"名:"+data[0]+"\t\t\t\t文件大小:"+data[2]+"MB"+"\t"+data[3]+"\t"+data[4]+"\t最后修改日期"+data[5]+"\t"+data[6]+"\t"+data[7]+"\n");
}
return sb.toString();
}
else return "文件或路径不存在";
}
public void dataDemo() {
jp1.add(shijian);
th = new Thread() {
public void run() {
while(true) {
Date d = new Date();
String s = DateFormat.getDateTimeInstance(
DateFormat.FULL,DateFormat.FULL,Locale.CHINA).format(d);
shijian.setText(s);
try {
Thread.sleep(1000);
} catch(Exception e) {
}
}
}
};
th.start();
}
public void showFile(){ //显示文件的方法
BufferedReader inFile = null;
String fileName = jtf.getText().trim();
String inLine;
try{
inFile = new BufferedReader(new FileReader(fileName));
jta.setText("");
while((inLine = inFile.readLine()) != null){
jta.append(inLine + '\n');
}
}catch(FileNotFoundException ex){
jtf.setText("找不到文件:"+fileName);
}catch(IOException ex){
System.out.println(ex.getMessage());
}finally{
try{
if(inFile != null)
inFile.close();
}catch(IOException ex){
System.out.println(ex.getMessage());
}
}
}
public String getTypeName(String s){
if(s.indexOf(".")==-1)
return "";
else{
String s1=s.substring(s.indexOf(".")+1,s.length());
return s1;
}
}
public static void main(String[] args){
try{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
}catch(Exception e){}
DirectoryFile test = new DirectoryFile();
}
}
作者;杨朝文
展开阅读全文