Java Tutorial/Development/Desktop

Материал из Java эксперт
Перейти к: навигация, поиск

Desktop Help Applications

import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class DesktopDemo {
  static Desktop desktop;
  public static void main(String[] args) {
    
    if (Desktop.isDesktopSupported()) {
      desktop = Desktop.getDesktop();
    } else {
      System.out.println("Desktop class is not supported");
      System.exit(1);
    }
    JMenuItem openItem = new JMenuItem("Open");
    JMenuItem editItem = new JMenuItem("Edit");
    JMenuItem printItem = new JMenuItem("Print");
    JMenuItem browseToItem = new JMenuItem("Go to www.jexp.ru");
    JMenuItem mailToItem = new JMenuItem("Email to a@java.ru");
    JMenu fileMenu = new JMenu("File");
    JMenu mailMenu = new JMenu("Email");
    JMenu browseMenu = new JMenu("Browser");
    
    openItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
          try {
            desktop.open(chooser.getSelectedFile().getAbsoluteFile());
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      }
    });
    fileMenu.add(openItem);
    
    editItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
          try {
            desktop.edit(chooser.getSelectedFile().getAbsoluteFile());
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      }
    });
    fileMenu.add(editItem);
    
    printItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
          try {
            desktop.print(chooser.getSelectedFile().getAbsoluteFile());
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      }
    });
    fileMenu.add(printItem);
    
    browseToItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          URI browseURI = new URI("www.jexp.ru");
          desktop.browse(browseURI);
        } catch (Exception ex) {
          System.out.println(ex.getMessage());
        }
      }
    });
    browseMenu.add(browseToItem);
    mailToItem.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        try {
          URI mailURI = new URI("mailto:support@java.ru");
          desktop.mail(mailURI);
        } catch (Exception ex) {
          System.out.println(ex.getMessage());
        }
      }
    });
    mailMenu.add(mailToItem);
    JMenuBar jMenuBar = new JMenuBar();
    jMenuBar.add(fileMenu);
    jMenuBar.add(browseMenu);
    jMenuBar.add(mailMenu);
    JFrame frame = new JFrame();
    frame.setTitle("Desktop Helper Applications");
    frame.setSize(300, 100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setJMenuBar(jMenuBar);
    frame.setVisible(true);
  }
}





Invoke the default editor to edit a file

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class Test {
  public static void main(String[] a) {
    try {
      Desktop desktop = null;
      if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
      }
       desktop.edit(new File("c:\\a.txt"));
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
}





Open a office word file with Java

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class Test {
  public static void main(String[] a) {
    try {
      Desktop desktop = null;
      if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
      }
       desktop.open(new File("c:\\a.doc"));
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
}





Open Mail client

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
public class Test {
  public static void main(String[] a)throws Exception {
      Desktop desktop = null;
      if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
      }
       desktop.mail();
  }
}





Send out email

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
public class Test {
  public static void main(String[] a)throws Exception {
      Desktop desktop = null;
      if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
      }
       desktop.mail("mailto", "a@a.net", null);
  }
}





Using system default printer to print a file out

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class Test {
  public static void main(String[] a) {
    try {
      Desktop desktop = null;
      if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
      }
       desktop.print(new File("c:\\a.txt"));
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
}





Using the Desktop class to launch a URL with default browser

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class UsingDesltopClassToLaunch {
  public static void main(String[] a) {
    try {
      URI uri = new URI("http://www.jexp.ru");
      Desktop desktop = null;
      if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
      }
      if (desktop != null)
        desktop.browse(uri);
    } catch (IOException ioe) {
      ioe.printStackTrace();
    } catch (URISyntaxException use) {
      use.printStackTrace();
    }
  }
}





Using the system default setting to open a file

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
public class Test {
  public static void main(String[] a) {
    try {
      Desktop desktop = null;
      if (Desktop.isDesktopSupported()) {
        desktop = Desktop.getDesktop();
      }
       desktop.open(new File("c:\\a.txt"));
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
}