JSPCN主页 | JSP空间 | 网站制作 | JSP下载 | JSP论坛 | JSP教程 | 关于JSPCN | 联系我们
JSP虚拟主机,jsp空间,java空间,java虚拟空间,详细请点击进入
做最专业的JSP中文网站 当前位置首页--JSP技术--文件操作  
文章搜索:
关键字 标题
  
JSP中文网内容管理系统(JCMS)
JSP虚拟主机
网络笔记本
网摘,图片,笔记收藏
虚拟服务器

JSPCN文章目录分类
JSP配置[219]JSP基础[136]
中文问题[69]上传问题[27]
JAVABEAN[46]数据库[212]
文件操作[126]图片声音[17]
JSP其他[57]时间相关[16]
JAVAMAIL[72]STRUTS[144]
开发工具[28]教程系列[157]
JSP实例[89]
JAVA基础[421]APPLET[78]
JAVA网络[179]Applica[115]
Servlet[98]XML[163]
J2ME[257]J2EE[374]
考试相关[63]JAVA线程[90]
EJB[261]Swing[26]
Java API[141]声音图片[28]
异常处理[33]JAVA实例[290]
JAVA类[139]SUN[89]
Hibernate[6]JMX[8]
Spring[34]
本版推荐文章 
本版热点文章 
相关文章链接 
 
[JAVA100例]017、文件对话框
作者:     文章来源:
访问次数:541次     加入时间:2007年03月05日

import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;
/**
 * <p>Title: 文件过滤器演示</p>
 * <p>Description: FileChooserDemo文件使用的文件过滤器</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Filename: MyFilter.java</p>
 * @version 1.0
 */


public class MyFilter extends FileFilter {
   private String files;
   public boolean accept(File f) {
        if (f.isDirectory()) {
            return true;
        }


        String extension = getExtension(f);
        if (extension != null) {
           
            if (extension.equals("java")) {//定义过滤Java文件
                    return true;
            } else {
                return false;
            }


        }


        return false;
    }


    //过滤器描述
    public String getDescription() {
        return "Java";
    }
/**
 *<br>方法说明:获取文件扩展名
 *<br>输入参数:
 *<br>返回类型:
 */
    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf(´.´);


        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
}


 


 


import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
/**
 * <p>Title: 文件对话框演示</p>
 * <p>Description: 演示打开文件对话框和保存文件对话框,使用了文件过滤。</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Filename: FileChooserDemo.java</p>
 * @version 1.0
 */


public class FileChooserDemo extends JPanel
                             implements ActionListener {
    static private final String newline = " ";
    JButton openButton, saveButton;
    JTextArea log;
    JFileChooser fc;


    public FileChooserDemo() {
        super(new BorderLayout());


        log = new JTextArea(15,40);
        log.setMargin(new Insets(10,10,10,10));
        log.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(log);


        //创建一个文件过滤,使用当前目录
        fc = new JFileChooser(".");
        //过滤条件在MyFilter类中定义
        fc.addChoosableFileFilter(new MyFilter());


        openButton = new JButton("打开文件",
                                 createImageIcon("images/Open16.gif"));
        openButton.addActionListener(this);


        saveButton = new JButton("保存文件",
                                 createImageIcon("images/Save16.gif"));
        saveButton.addActionListener(this);


        //构建一个面板,添加“打开文件”和“保存文件”
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(openButton);
        buttonPanel.add(saveButton);


        add(buttonPanel, BorderLayout.PAGE_START);
        add(logScrollPane, BorderLayout.CENTER);
    }
/**
 *<br>方法说明:事件处理
 *<br>输入参数:
 *<br>返回类型:
 */
    public void actionPerformed(ActionEvent e) {


        //当点击“打开文件”按钮
        if (e.getSource() == openButton) {
            int returnVal = fc.showOpenDialog(FileChooserDemo.this);


            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                //在这里添加一些对文件的处理
                log.append("打开文件: " + file.getName() + newline);
            } else {
                log.append("打开文件被用户取消!" + newline);
            }


        //点击“保存文件”按钮
        } else if (e.getSource() == saveButton) {
            int returnVal = fc.showSaveDialog(FileChooserDemo.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                //在这里添加一些对文件的处理
                log.append("保存文件: " + file.getName()  + newline);
            } else {
                log.append("保存文件被用户取消!" + newline);
            }
        }
    }
/**
 *<br>方法说明:获取图像对象
 *<br>输入参数:String path 图片路径
 *<br>返回类型:ImageIcon 图片对象
 */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = FileChooserDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn´t find file: " + path);
            return null;
        }
    }


    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);


        //创建窗体
        JFrame frame = new JFrame("FileChooserDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //创建一个面板
        JComponent newContentPane = new FileChooserDemo();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);


        //显示窗体
        frame.pack();
        frame.setVisible(true);
    }
}

 
JSP虚拟主机 | JSP空间 | JSP主机 | JSP双线虚拟主机 | JAVA空间 | JAVA虚拟主机 | 虚拟服务器 | JSP 虚拟服务器 | VPS
Virtual Private Server | JAVA虚拟服务器 | VM服务器 | VHOST | 虚拟操作系统 | JSP论坛 | JAVA论坛 | JSP站点论坛
Copyright © 2002-2005 JSPCN.net. All rights reserved.
JSP中文网    备案序号:蜀ICP备05001583号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号