有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    使用桌面。在按钮点击处理程序中打开(文件)
    SE7 Desktop

  2. # 2 楼答案

    创建一个File对象(或从JFileChooser中获取一个)对应于要打开的内容,然后使用Desktop.open

    Desktop desktop = Desktop.getDesktop();
    if(file.exists()) 
       desktop.open(file);
    

    该方法说明:

    Launches the associated application to open the file

    还要注意可能出现的例外情况:

    NullPointerException - if file is null

    IllegalArgumentException - if the specified file doesn't exist

    UnsupportedOperationException - if the current platform does not support the Desktop.Action.OPEN action

    IOException - if the specified file has no associated application or the associated application fails to be launched

    SecurityException - if a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file, or it denies the AWTPermission("showWindowWithoutWarningBanner") permission, or the calling thread is not allowed to create a subprocess

  3. # 3 楼答案

    有一种方法可以做到这一点:

    java.awt.Desktop.getDesktop().open(file);
    
    public void actionPerformed(ActionEvent e) {
    if (Desktop.isDesktopSupported()) {
        try {
            File myFile = new File("guide/guide.pdf");
            Desktop.getDesktop().open(myFile);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null,
                    LocalizationService.localizeString("no_pdf"),
                    LocalizationService.localizeString("no_pdf_tit"),
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }
    

    }

    你可以在这里找到例子http://www.journaldev.com/864/how-to-open-a-file-in-java