Introduction to the use of desktop classes in Java

Source: Internet
Author: User

a new class--desktop was added after Jdk1.6, which is explained in the JDK:

The Desktop class allows a Java application to launch associated applications registered on the native desktop to ha Ndle 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.
The meaning of this passage is:The Desktop class allows a Java application to launch another local application to handle a URI or file request, which contains several methods:1. Start the user's default browser to display the specified URI link2. Start User default mail client send URI specified message3. Launch a registered application (locally installed application) to open, edit or print a specified file

Below, give a test code to illustrate the function of this class and how to use it, with comments in the code:

    Package com.brucezhang.desktop;      Import Java.awt.Desktop;      Import Java.io.File;                        Import Java.net.URI;                public class Desktoptest {private static desktop desktop; Open Web page using default browser public static void browse () {if (desktop.isdesktopsupported ()) {des                  Ktop = Desktop.getdesktop (); try {//uri Specifies the address of the Web page desktop.browse (The New URI ("Http://blog.csdn.net/dlutbrucezhang?v                  Iewmode=contents "));                  } catch (Exception e) {//Todo:handle Exception e.printstacktrace (); }}}//edit file public static void edit () {if (deskto                  P.isdesktopsupported ()) {desktop = Desktop.getdesktop ();                  try {desktop.edit (new File ("D:\\brucezhang.txt")); } catch (Exception e)                  {//Todo:handle exception e.printstacktrace ();              }}}//Open the file, it is similar to the process of editing the file, you can see the file display 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 (); }}}//print the specified file public static void print () {if (D                  Esktop.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 ();   }            }

Introduction to the use of desktop classes in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.