0

我有一个使用 JFileChooser 的程序。简而言之,完整的程序是一个允许用户操作 PNG 和 JPG 的 GUI。我想让 JFileChooser 立即打开图片目录(Windows)。当用户打开他们的 JFileChooser 时,它会直接打开图片库 C:\Users\(USER)\Pictures

此外,最好只显示特定类型的文件(PNG 和 JPG)。许多程序似乎都可以做到这一点;只允许选择特定文件。JFileChooser 是否允许这样的事情?目前,我正在使用一种非常不可靠的绕行方法来拒绝非 PNG/JPG。

以下是指GUI的“浏览”按钮,用户将在其中选择要编辑的图片并将其显示在屏幕上。

    try {
       int val = filec.showOpenDialog(GridCreator.this);
       if(val==JFileChooser.APPROVE_OPTION) {
          File unfiltered_picture = filec.getSelectedFile();
          //get the extension of the file
          extension=unfiltered_picture.getPath();
          int index=extension.indexOf(".");
          extension=extension.substring(index+1, extension.length());
          //if the file is not jpg, png, or jpeg, reject it and send a message to the user.
          if(!extension.matches("[jJ][pP][gG]") && !extension.matches("[pP][nN][gG]") && !extension.matches("[jJ][pP][eE][gG]")) {
             JOptionPane.showMessageDialog(null,
                                           "cannot load file. File must be of type png, jpeg, or jpg. \n Your file is of type " + extension,
                                            "Error: improper file",
                                            JOptionPane.OK_OPTION);
           //if the file is of the proper type, display it to the user on the img JLabel.
           } else {
              finalImage = ImageIO.read(unfiltered_picture);
              ImageIcon imgIcon = new ImageIcon();
              imgIcon.setImage(finalImage);
              img.setIcon(imgIcon);
              img.invalidate();
              h_divide.setValue(0);
              v_divide.setValue(0);
           }
       }
   } catch(IOException exception) {
        exception.printStackTrace();
   }

谢谢你。

4

2 回答 2

9

您需要JFileChooser使用要开始的目录构建您的目录,然后FileFilter在设置可见之前将 a 传递给它。

    final JFileChooser fileChooser = new JFileChooser(new File("File to start in"));
    fileChooser.setFileFilter(new FileFilter() {
        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            final String name = f.getName();
            return name.endsWith(".png") || name.endsWith(".jpg");
        }

        @Override
        public String getDescription() {
            return "*.png,*.jpg";
        }
    });
    fileChooser.showOpenDialog(GridCreator.this);

此示例过滤以“.png”或“.jpg”结尾的文件。

于 2013-04-11T17:11:09.380 回答
1

把它全部放在一个简洁的形式中,这是一个灵活的文件选择器例程。它指定初始目录和文件类型,并将结果提供为文件或完整路径名。您可能还希望通过将 setLookAndFeel 命令放在程序的 Main 入口点,将整个程序设置为本地界面模式。

String[] fileChooser(Component parent, String dir, String typeFile) {
    File dirFile = new File(dir);
    JFileChooser chooser = new JFileChooser();
    // e.g. typeFile = "txt", "jpg", etc.
    FileNameExtensionFilter filter = 
        new FileNameExtensionFilter("Choose a "+typeFile+" file",
            typeFile); 
    chooser.setFileFilter(filter);
    chooser.setCurrentDirectory(dirFile);
    int returnVal = chooser.showOpenDialog(parent);

    String[] selectedDirFile = new String[2];
    if(returnVal == JFileChooser.APPROVE_OPTION) {
        // full path
        selectedDirFile[0] = chooser.getSelectedFile().getPath();
        // just filename
        selectedDirFile[1] = chooser.getSelectedFile().getName();
    }

    return selectedDirFile;
 }

try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
    e.printStackTrace();
}
于 2015-10-04T06:49:42.423 回答