JDesktop的使用

        在跨平台领域书写JAVA程序时,一个重要的问题就是无法直接访问操作系统的常用程序。比如用Java写好报表,总希望能自动用已注册的程序打开相应的文件(如.doc)。以前没有JDesktop时,可以用System.execute()方法直接启动特定的程序,但这种硬编码的方法相当不可靠。谁会知道客户机是是安装了MS WORD还是OpenOffice呢?JDK6中的JDesktop解决了所有问题。

        如查你用打开系统的浏览器,或者记事本,或是邮件管理器等,JDesktop可以满足你的需求:

/**/ /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 
*/


package  javaapplication1;

import  java.awt.Desktop;
import  java.io.IOException;
import  java.net.URISyntaxException;
import  java.util.logging.Level;
import  java.util.logging.Logger;

/** */ /**
 *
 * 
@author ljm-home
 
*/

public   class  JDesktopTest  ... {
    
//定义Desktop
    Desktop desktop=null;
    
    
public JDesktopTest()...{
        
//判断是否支持Desktop
        if(Desktop.isDesktopSupported())
            desktop
=Desktop.getDesktop();
    }

    
    
//用操作系统的记事本打开文件(Notepan、VI、GEdit等)
    public void editTextFile(java.io.File file)...{
        
//如果支持Desktop,再判断是否支持edit
        if(desktop!=null && desktop.isSupported(Desktop.Action.EDIT))...{
            
try ...{
                desktop.edit(file);
            }
 catch (IOException ex) ...{
                Logger.getLogger(JDesktopTest.
class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

    
    
//打操作系统的浏览器(IE,Firefox,Opera等)
    public void lannchSystemBrowser(java.net.URI uri)...{
        
//如果支持Desktop,再判断是否支持launchBrowser
        if(desktop!=null && desktop.isSupported(Desktop.Action.BROWSE))...{
            
try ...{
                desktop.browse(uri);
            }
 catch (IOException ex) ...{
                Logger.getLogger(JDesktopTest.
class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

    
    
//打操作系统的邮件管理器
    public void sendEmail(java.net.URI uri)...{
        
//如果支持Desktop,再判断是否支持mail
        if(desktop!=null && desktop.isSupported(Desktop.Action.MAIL))...{
            
try ...{
                desktop.mail(uri);
            }
 catch (IOException ex) ...{
                Logger.getLogger(JDesktopTest.
class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

    
    
//用操作系统的浏览器打开一个目录
    public void openFile(java.io.File file)...{
        
//如果支持Desktop,再判断是否支持open
        if(desktop!=null && desktop.isSupported(Desktop.Action.OPEN))...{
            
try ...{
                desktop.open(file);
            }
 catch (IOException ex) ...{
                Logger.getLogger(JDesktopTest.
class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

    
    
//用操作系统的默认打印机打印文件
    public void printFile(java.io.File file)...{
        
//如果支持Desktop,再判断是否支持print
        if(desktop!=null && desktop.isSupported(Desktop.Action.PRINT))...{
            
try ...{
                desktop.print(file);
            }
 catch (IOException ex) ...{
                Logger.getLogger(JDesktopTest.
class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

    
    
/** *//**
     * 
@param args the command line arguments
     
*/

    
public static void main(String[] args) ...{
        
try ...{
            JDesktopTest jdt 
= new JDesktopTest();
            
//打开项目目录下的test.txt文件
            jdt.editTextFile(new java.io.File("test.txt"));
            Thread.sleep(
500);
            
            
//打开http://www.csdn.net网页
            jdt.lannchSystemBrowser(new java.net.URI("http://www.csdn.net"));
            Thread.sleep(
500);
            
            
//给Test@163.com发邮件
            jdt.sendEmail(new java.net.URI("mailto:Test@163.com"));
            Thread.sleep(
500);
            
            
//打开D:TDDOWNLOAD网页
            jdt.openFile(new java.io.File("d:/TDDOWNLOAD"));
            Thread.sleep(
500);
            
            
//打印test.txt
            jdt.printFile(new java.io.File("test.txt"));
        }
 catch (URISyntaxException ex) ...{
            Logger.getLogger(JDesktopTest.
class.getName()).log(Level.SEVERE, null, ex);
        }
 catch (InterruptedException ex) ...{
            Logger.getLogger(JDesktopTest.
class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值