Java中的DeskTop類使用介紹

jopen 11年前發布 | 86K 次閱讀 DeskTop Java開發

  在Jdk1.6以后新增加了一個類--DeskTop,在JDK中它的解釋是這樣的:

The Desktop class allows a Java application to launch associated applications registered on the native desktop to handle a URI or a file.

Supported operations include:

  • launching the user-default browser to show a specified URI;
  • launching the user-default mail client with an optional mailto URI;
  • launching a registered application to open, edit or print a specified file.  
這段話的意思是:
      DeskTop類允許一個Java應用程序啟動本地的另一個應用程序去處理URI或文件請求,這個類中包含了如下的幾個方法:
   1.啟動用戶默認的瀏覽器顯示指定的URI鏈接
   2.啟動用戶默認的郵件客戶端發送URI指定的郵件
   3.啟動一個注冊應用程序(本地安裝了的應用程序)去打開,編輯或打印一個指定的文件

下面,給出一段測試代碼說明這個類的功能和使用方法,代碼中附有注釋:

    package com.brucezhang.desktop;  

    import java.awt.Desktop;  
    import java.io.File;  
    import java.net.URI;  



    public class DeskTopTest {  

        private static Desktop desktop;  

        //使用默認的瀏覽器打開網頁  
        public static void browse(){  
            if (Desktop.isDesktopSupported()) {  
                desktop = Desktop.getDesktop();  
                try {  
                    //URI指定網頁的地址  
                    desktop.browse(new URI("http://blog.csdn.net/dlutbrucezhang?viewmode=contents"));  
                } catch (Exception e) {  
                    // TODO: handle exception  
                    e.printStackTrace();  
                }  
            }  
        }  

        //編輯文件  
        public static void edit(){  
            if (Desktop.isDesktopSupported()) {  
                desktop = Desktop.getDesktop();  
                try {  
                    desktop.edit(new File("D:\\BruceZhang.txt"));  
                } catch (Exception e) {  
                    // TODO: handle exception  
                    e.printStackTrace();  
                }  
            }  
        }  

        //打開文件,它和編輯文件的過程類似,都是能看到文件的顯示  
        public static void open() {  
            if (Desktop.isDesktopSupported()) {  
                desktop = Desktop.getDesktop();  
                try {  
                    desktop.open(new File("D:\\BruceZhang.txt"));  
                } catch (Exception e) {  
                    // TODO: handle exception  
                    e.printStackTrace();  
                }  
            }  
        }  

        //打印指定的文件  
        public static void print() {  
            if (Desktop.isDesktopSupported()) {  
                desktop = Desktop.getDesktop();  
                try {  
                    desktop.print(new File("D:\\BruceZhang.txt"));  
                } catch (Exception e) {  
                    // TODO: handle exception  
                    e.printStackTrace();  
                }  
            }  
        }  

        /** 
         * @param args 
         */  
        public static void main(String[] args) {  
            // TODO Auto-generated method stub  

            browse();  
            edit();  
            open();  
            print();  
        }  

    }  

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!