API--16-- Desktop桌面类

Desktop 桌面类

Desktop 类是java的桌面类,可以启动程序,是jdk 1.6 时发布的,位于 java.awt 包下。

在这里插入图片描述

支持以下几种功能:

  1. open 打开文件
  2. edit 编辑文件
  3. print 打印文件
  4. mail 发送邮件
  5. browse 浏览器打开网址

代码中体现为: Action 枚举

在这里插入图片描述

单例模式的, 私有化构造器

在这里插入图片描述

需要静态方法 getDesktop() 来构建实例化对象

Desktop desktop=Desktop.getDesktop();

在使用的时候,最好先验证一下,PC 环境 是否支持 Desktop 类的使用

在这里插入图片描述

if(Desktop.isDesktopSupported()){

}

案例:

browse 浏览器验证

  • 调用电脑默认的浏览器,进行访问网址。
    @Test
    public void testBrowse(){
        if(Desktop.isDesktopSupported()){ //是否支持桌面
            Desktop desktop=Desktop.getDesktop();
            try {
                // 构建正确的网址
                desktop.browse(new URI("https://www.baidu.com/"));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }
    }

在这里插入图片描述

  • URL 时,有时候会造成 编码上的错误, 需要转换一下。
  • 有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了。编码的格式为:%加字符的ASCII码,即一个百分号%,后面跟对应字符的ASCII(16进制)码值。例如空格的编码值是"%20"。
    在这里插入图片描述

open 打开文件

  • 可以打开 常见的文件,如 普通文件(包括常见的.txt 和.word 之类的),图片,视频,应用。
@Test
	public void testOpen(){
		if(Desktop.isDesktopSupported()){ //支持桌面
			Desktop desktop=Desktop.getDesktop();
			try {
				//desktop.open(new File("D:"+File.separator+"QQ.java"));  // 打开普通文件
				//desktop.open(new File("D:"+File.separator+"image.jpg")); //打开图片
				//desktop.open(new File("D:"+File.separator+"servlet.mp4")); //打开视频
				desktop.open(new File("I:\\Program Files\\Tencent\\QQ\\Bin\\QQScLauncher.exe")); //打开应用。
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

edit 编辑文件

  • 可以编辑普通的文件,会先打开, 但是有些文件是不能编辑的,如mp4 视频文件和应用文件。
/ 测试编辑文件
	@Test
	public void testEdit(){
		if(Desktop.isDesktopSupported()){ //支持桌面
			Desktop desktop=Desktop.getDesktop();
			try {
				desktop.edit(new File("D:"+File.separator+"QQ.java"));
				//这样,就是错误的。
				//desktop.edit(new File("D:"+File.separator+"servlet.mp4"));
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

print 打印文件

  • 只是进入到打印的动作,并不是真正与打印机关联一键打印了
@Test
	public void testPrint(){
		if(Desktop.isDesktopSupported()){ //支持桌面
			Desktop desktop=Desktop.getDesktop();
			try {
				//会调动打印程序,进行打印的界面,而不是自动打印成功了。
				desktop.print(new File("D:"+File.separator+"word.docx"));
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

测试时,会进入到

在这里插入图片描述

mail 邮件发送

  • 需要安装邮件类软件,如 火狐邮箱,office 自带的 outlook 等。
@Test
	public void testMail(){
		if(Desktop.isDesktopSupported()){ //支持桌面
			Desktop desktop=Desktop.getDesktop();
			try {
				desktop.mail();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

在这里插入图片描述

mail(URI uri) 这种参数的形式,约束可以查看网址:

@Test
	public void testMail(){
		if(Desktop.isDesktopSupported()){ //支持桌面
			Desktop desktop=Desktop.getDesktop();
			try {
				try {
				//	desktop.mail(new URI("mailto:1290513799@qq.com?subject='测试服务器是否好用'&body='两个蝴蝶飞'"));
				// '' 会当成内容,所以应该去除。
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			} catch (URISyntaxException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值