繁体   English   中英

使用NetBeans Java和Windows在jframe中打开pdf文件

[英]Opening a pdf file in jframe using netbeans java and windows

我创建了一个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时出现问题找不到指定的模块。

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

我只会使用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();
            }
        });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM