zoukankan      html  css  js  c++  java
  • Java中的DeskTop类使用介绍

      在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 optionalmailto 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();  
            }  
          
        }  
  • 相关阅读:
    浅谈模块化开发
    用gulp搭建并发布自己的cli脚手架
    取值运算符新用法
    vue双向绑定之简易版
    获取对象属性之括号方式
    前端格式化工具之Prettier
    git操作之摘樱桃
    Sort
    MongoDB
    项目使用本地的包
  • 原文地址:https://www.cnblogs.com/android-blogs/p/5545736.html
Copyright © 2011-2022 走看看