【问题标题】:PDF specific File explorerPDF 特定文件资源管理器
【发布时间】:2015-03-10 11:02:45
【问题描述】:

我正在开发一个特定于 PDF 的文件浏览器,在堆栈溢出的帮助下,我取得了很大的进步,但我的代码又遇到了一个问题。我已经制作了使此文件资源管理器功能所需的所有元素,但是这两个类无法正常通信。这是我的代码 打包pdfView;

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

import javax.swing.*;
import javax.swing.filechooser.*;

public class FileChooser2 extends JPanel implements ActionListener {

     static private final String newline = "\n";
        JButton openButton, saveButton;
        JTextArea log;
        JFileChooser fc;

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

            log = new JTextArea(5,20);
            log.setMargin(new Insets(5,5,5,5));
            log.setEditable(false);
            JScrollPane logScrollPane = new JScrollPane(log);

            //Create a file chooser
            fc = new JFileChooser();

            //Create the open button.
            openButton = new JButton("Open a File...");
            openButton.addActionListener(this);

            JPanel buttonPanel = new JPanel(); 
            buttonPanel.add(openButton);

            //Add the buttons and the log to this panel.
            add(buttonPanel, BorderLayout.PAGE_START);
            add(logScrollPane, BorderLayout.CENTER);
        }

        public void actionPerformed(ActionEvent e) {

            //Handle open button action.
            if (e.getSource() == openButton) {
                int returnVal = fc.showOpenDialog(FileChooser2.this);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    log.append("Opening: " + file.getName() + "." + newline);
                } else {
                    log.append("Open command cancelled by user." + newline);
                }
                log.setCaretPosition(log.getDocument().getLength());
            }
        }

        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = FileChooser2.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("PDF Viewer");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //Add content to the window.
            frame.add(new FileChooser2());

            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }

        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    UIManager.put("swing.boldMetal", Boolean.FALSE);
                    createAndShowGUI();
                } 
            });
        }
        public class pdfFilter {
            //PDF Filter using extension
             public boolean accept(File f) {
                 if (f.isDirectory()){
                    return true;
                }
                String extension = Utils.getExtension(f);
                if (extension != null) {
                    if (extension.equals(Utils.pdf)){
                        return true;
                    } else {
                        return false;
                    }
                }
                return false;
            }

        }
    }

这是应该设置 pdf 过滤器的 utils.java 类

package pdfView;
import java.io.File;


public class Utils {
    //get file name work with pdfFilter.java
    public final static String pdf = "PDF";

    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;
    }

}

【问题讨论】:

  • 您正在将扩展名转换为小写,然后将其与“PDF”(大写)进行比较。
  • 我没看懂你在说什么。
  • 在 Utils.getExtension() 中,您可以这样做:ext = s.substring(i+1).toLowerCase();。但 Utils.pdf 是“PDF”

标签: java file pdf viewer


【解决方案1】:

改变这一行:

extension.equals(Utils.pdf)...

收件人:

extension.equalsIgnoreCase(Utils.pdf)

您正在将“pdf”与“PDF”进行比较。这是一个ASCII比较。如果你想要字母数字比较使用上面的代码:)

【讨论】:

  • 好的,我已经完成了这个操作,但它仍然不是仅通过 .pdf 扩展名搜索文件
  • 你从来没有设置过滤器。在你的构造函数中尝试fc.setFileFilter(new pdfFilter())
  • 对不起,如果我听起来有点笨拙,但我将把这段代码准确地放在哪里。我对java有点陌生
  • 在您的构造函数 FileChooser2() 中,就在您实例化 fc 的那一行之后。
  • 它在“setFileFilter”行上给了我这个错误错误:“JFileChooser 类型中的方法 setFileFilter(FileFilter) 不适用于参数 (FileChooser2.pdfFilter)”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多