使用netbeans java和windows在jframe中打开pdf文件

时间:2015-03-30 20:27:47

标签: java swing pdf netbeans

我创建了一个netbeans GUI,我试图在点击一个按钮时打开一个pdf。 它不适合我,这是我的代码

private void openBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
try{
    Runtime.getRuntime().exec(" rundll32 url.d11,FileProtocolHandler"+"C:\\Users\\andre\\Downloads\\MathsLeavingCertApp\\MathsLeavingCertApp - Main\\MathsLeavingCertApp - Main\\2014MathsHL");

    } 
     catch(Exception e)
    {
    JOptionPane.showMessageDialog(null,"Error");
    }



}     

这就是我得到的错误

启动url.d11时出现问题 找不到指定的模块。

任何人都不知道为什么它不起作用

1 个答案:

答案 0 :(得分:2)

我只会使用Dektop.getDesktop().openFile(File)

请参阅此示例说明:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileFilter;

public class TestOpenFile {
    JFileChooser chooser = new JFileChooser();

    protected void initUI() {
        JFrame frame = new JFrame("test");
        Container cp = frame.getContentPane();
        cp.setLayout(new BorderLayout());
        final JButton chooseFile = new JButton("Select file...");
        chooser.setFileFilter(new FileFilter() {

            @Override
            public String getDescription() {
                return "*.pdf";
            }

            @Override
            public boolean accept(File f) {
                return f.isFile() && f.getName().toLowerCase().endsWith(".pdf");
            }
        });
        chooseFile.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int r = chooser.showOpenDialog(chooseFile);
                if (r == JFileChooser.APPROVE_OPTION) {
                    try {
                        Desktop.getDesktop().open(chooser.getSelectedFile());
                    } catch (IOException e1) {
                        JOptionPane.showMessageDialog(chooseFile, "Could not open file " + chooser.getSelectedFile().getAbsolutePath());
                    }

                }
            }
        });
        cp.add(chooseFile);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestOpenFile().initUI();
            }
        });
    }
}