java Desktop 使用样例

来源:互联网 发布:java string 添加空格 编辑:程序博客网 时间:2024/06/05 11:43
package test;


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


public class DeskTopTest {
    
    private static Desktop desktop;
    
    public static void main(String[] args) {
        // browse();
        edit();
        open();
        // print();
    }
    
    // 使用默认的浏览器打开网页
    public static void browse() {
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
            try {
                desktop.browse(new URI("www.baidu.com"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    // 使用设置的默认编辑器打开文件
    public static void edit() {
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
            try {
                desktop.edit(new File("D:\\DesktopTest.txt"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    // 使用系统默认的编辑器打开文件
    public static void open() {
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
            try {
                desktop.open(new File("D:\\DesktopTest.txt"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    // 使用配置的默认打印机打印文件
    public static void print() {
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
            try {
                desktop.print(new File("D:\\DesktopTest.txt"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
0 0
原创粉丝点击