J2se单一文件类型选择对话框

本文提供了一个使用 Java Swing 中 JFileChooser 的示例程序,演示了如何创建一个文件选择器对话框,包括打开和保存文件的功能,并通过自定义文件过滤器实现特定类型的文件筛选。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JFileChooserDemo:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

//文件选择器演示

public class JFileChooserDemo extends JFrame {
 private JFileChooser chooser;  //文件选择器
 private JButton button;  //选择文件按钮
 private JComboBox comboBox;  //用于设定文件对话框作用(打开还是保存文件)

 public JFileChooserDemo() {
  super("JFileChooser 演示");  //调用父类构造函数
  Container contentPane = getContentPane();  //得到容器
  contentPane.setLayout(new FlowLayout());  //设置布局管理器为Flowlayout
        chooser=new JFileChooser();  //初始化文件选择器
        button = new JButton("选择文件");  //初始化按钮
        comboBox=new JComboBox();  //初始化组合框
        comboBox.addItem("打开");  //增加组合框列表内容
        comboBox.addItem("保存");
        contentPane.add(comboBox);  //增加组件到容器
  contentPane.add(button);

  button.addActionListener(new ActionListener() {  //按钮事件处理
      public void actionPerformed(ActionEvent e) {
       int state;  //文件选择器返回状态
       chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter());  //移去所有文件过滤器
       chooser.addChoosableFileFilter(new MyFileFilter("gif","图像文件"));   //增加文件过滤器,接爱gif文件

       if (comboBox.getSelectedIndex()==0)  //组合框为"打开"
          state=chooser.showOpenDialog(null);   //显示打开文件对话框
       else
          state=chooser.showSaveDialog(null);  //显示保存文件对话框
       File file = chooser.getSelectedFile();  //得到选择的文件

       if(file != null && state == JFileChooser.APPROVE_OPTION) {  //选择了文件并点击了打开可保存按钮
          JOptionPane.showMessageDialog(null, file.getPath()); //显示提示信息
       }
       else if(state == JFileChooser.CANCEL_OPTION) {  //点击了撤销按钮
         JOptionPane.showMessageDialog(null, "退出!");  //显示提示信息
       }
       else if(state == JFileChooser.ERROR_OPTION) {
         JOptionPane.showMessageDialog(null, "错误!");  //显示提示信息
       }
  }
  });

  this.setSize(200,100);  //设置窗口大小
  this.setVisible(true);  //设置窗口可见
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //关闭窗口时退出程序
 }

 public static void main(String args[]) {
  new JFileChooserDemo();
 }
}

MyFileFilter:

import java.io.File;
import javax.swing.filechooser.FileFilter;

//文件过滤器

public class MyFileFilter extends FileFilter {

 String ends;  //文件后缀
 String description;  //文件描述文字

 public MyFileFilter(String ends, String description) { //构造函数
  this.ends = ends;  //设置文件后缀
  this.description=description;  //设置文件描述文字
 }

 public boolean accept(File file) {  //重载FileFilter中的accept方法
  if (file.isDirectory())  //如果是目录,则返回true
   return true;
  String fileName = file.getName();  //得到文件名称
     if (fileName.toUpperCase().endsWith(ends.toUpperCase()))  //把文件后缀与可接受后缀转成大写后比较
       return true;
        else
          return false;
   }

   public String getDescription() {  //返回文件描述文字
      return description;
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值