JFileChooser文件类型gif和jpg的过滤

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

public class test extends JFrame {
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) throws Exception {
		new ConfigFrame("保存文件");
	}
}

class ConfigFrame extends JFrame {
	private static final long serialVersionUID = 1L;

	public ConfigFrame(String title) {
		//设置窗口属性
		final int width = 300;
		final int height = 200;
		final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		final int left = (screen.width - width) / 2;
		final int top = (screen.height - height) / 2;
		this.setLocation(left, top);
		this.setSize(width, height);
		this.setTitle(title);

		//添加组件
		JPanel panel = new JPanel();
		this.add(panel);
		JButton openBtn = new JButton("打开");
		JButton saveBtn = new JButton("保存");
		panel.add(openBtn);
		panel.add(saveBtn);

		final JFileChooser chooser = new JFileChooser("."); //在当前目录下,创建文件选择器

		JpgFileFilter jpgFilter = new JpgFileFilter(); //jpg过滤器
		GifFileFilter gifFilter = new GifFileFilter(); //gif过滤器
		chooser.addChoosableFileFilter(jpgFilter); //加载jpg文件过滤器
		chooser.addChoosableFileFilter(gifFilter); //加载gif文件过滤器

		chooser.setFileFilter(jpgFilter); //设置默认的文件管理器。如果不设置,则最后添加的文件过滤器为默认过滤器

		openBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int returnVal = chooser.showOpenDialog(ConfigFrame.this); //显示窗口

				if (returnVal == JFileChooser.APPROVE_OPTION) {
					String filepath = chooser.getSelectedFile().getPath();
					String filename = chooser.getSelectedFile().getName();

					System.out.println("path:" + filepath);
					System.out.println("name:" + filename);
				}

			}
		});

		saveBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				chooser.setSelectedFile(new File("picture.jpg")); //设置保存时的,默认文件名			

				int returnVal = chooser.showSaveDialog(ConfigFrame.this); //显示保存文件窗口

				if (returnVal == JFileChooser.APPROVE_OPTION) {
					String filepath = chooser.getSelectedFile().getPath();
					String filename = chooser.getSelectedFile().getName();
				}

			}
		});

		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

class JpgFileFilter extends FileFilter {
	public String getDescription() {
		return "*.jpg";
	}

	public boolean accept(File file) {
		String name = file.getName();
		return name.toLowerCase().endsWith(".jpg");
	}
}

class GifFileFilter extends FileFilter {
	public String getDescription() {
		return "*.gif";
	}

	public boolean accept(File file) {
		String name = file.getName();
		return name.toLowerCase().endsWith(".gif");
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值