JFileChooser过滤器 [英] JFileChooser filters

查看:166
本文介绍了JFileChooser过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的程序中放了一个 JFileChooser ,但这只需要图片。所以我决定添加过滤器:

I am putting a JFileChooser in my program, but that only takes images. So I decided to add filters:

import javax.swing.*;

public class fileChooser {

 public static void main(String[] args) {
    JPanel panel = new JPanel();

    final JFileChooser fc = new JFileChooser();
    int file = fc.showOpenDialog(panel);
    fc.addChoosableFileFilter(new ImageFilter());
    fc.setAcceptAllFileFilterUsed(false);
 }
}

我直接从Java教程那里得到了。但Eclipse强调以下内容为错误:

I got that straight from the Java tutorials. But Eclipse underlines the following as an error:

fc.addChoosableFileFilter(new ImageFilter());
fc.setAcceptAllFileFilterUsed(false);

有什么建议吗?

推荐答案


我在程序中放了一个JFileChooser,但只拍摄图片。

I am putting a JFileChooser in my program, but that only takes images.

对于该操作系统上该JRE支持的类型列表,请使用 ImageIO

For a list of types supported by that JRE on that OS, use ImageIO.

FileFilter imageFilter = new FileNameExtensionFilter(
    "Image files", ImageIO.getReaderFileSuffixes());



看到的类型 - Java 1.6 / Windows 7



Types seen - Java 1.6/Windows 7

bmp
jpg
jpeg
wbmp
png
gif

注意:不要对该列表进行硬编码!它可能会从版本更改为版本,OS可能会更改为操作系统。例如。

Note: don't hard-code that list! It might change from version to version, and OS to OS. E.G.


  1. 我对Windows支持加载BMP并不感到惊讶,但这是否会出现在Mac上?

  2. 看到 WBMP 提醒我这样的存在格式!

  1. I am not surprised that Windows has support to load BMP, but does that come up in a Mac?
  2. Seeing WBMP alerted me to the existence of such a format!

如果 jai 已安装。

这篇关于JFileChooser过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆