java 调用桌面程序

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

 /** 

  * Java1.6.0实现调用操作平台桌面系统 

  * Desktop类将获得操作平台的桌面系统,以便使用系统默认浏览器、编辑器、邮件、打印等 

  * 一堆按钮摆在一起不大好看,懒的布局了,大家能看明白就成,打开文件、编辑文件和打印文件需要先按“浏览”按钮,选择一个文件后才行。 

  * 

  * @author 五斗米 <如转载请保留作者和出处> 

  * @blog http://blog.csdn.net/mq612 

  */

 public class DesktopDemo extends JFrame { 

     private JPanel pane = null; 

     private JLabel label = null; // 显示信息的标签 

     private JButton [] button = null; // 启动平台默认程序的按钮 

     private Desktop desktop = null; // 本操作平台的桌面系统实例 

     private JTextField text = null; // 显示文件地址的TextField 

     private JButton b = null; // 浏览文件的按钮 

     private JFileChooser fc = null; // 需要浏览文件 

     private File file = null; // 文件 

      

     public DesktopDemo() { 

         super("Java1.6.0实现调用操作平台桌面系统"); 

         try { 

             // 将LookAndFeel设置成Windows样式 

             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 

         } catch (Exception ex) { 

             ex.printStackTrace(); 

         } 

         fc = new JFileChooser(); 

         pane = new JPanel(); 

         label = new JLabel("本操作平台不支持桌面系统"); // 默认标签文字为不支持 

         pane.add(label); 

         button = new JButton[5]; 

         button[0] = new JButton("默认浏览器"); 

         button[1] = new JButton("默认邮件"); 

         button[2] = new JButton("默认程序打开文件"); 

         button[3] = new JButton("默认程序编辑文件"); 

         button[4] = new JButton("打印文件"); 

         for(int i = 0; i < button.length; i++){ // 使按钮暂不可用 

             button[i].setEnabled(false); 

         } 

         pane.add(button[0]); 

         pane.add(button[1]); 

         text = new JTextField(30); 

         text.setEditable(false); // 不可编辑 

         b = new JButton("浏览"); // 使按钮暂不可用 

         b.setEnabled(false); 

         pane.add(text); 

         pane.add(b); 

         pane.add(button[2]); 

         pane.add(button[3]); 

         pane.add(button[4]); 

         desktop = Desktop.getDesktop(); // 返回本操作平台的桌面系统 

         if(desktop.isDesktopSupported()){ // 如果该操作平台支持桌面系统 

             this.desktop(); 

         } 

         this.getContentPane().add(pane); 

         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

         this.setSize(400, 200); 

         this.setVisible(true); 

     } 

     /** 

      * 桌面系统 

      */

     private void desktop(){ 

         label.setText("本操作平台支持桌面系统"); 

         for(int i = 0; i < button.length; i++){ // 使按钮可用 

             button[i].setEnabled(true); 

         } 

         b.setEnabled(true); 

         b.addActionListener(new ActionListener() { 

             public void actionPerformed(ActionEvent e) { 

                 openFile(); 

             } 

         }); 

         button[0].addActionListener(new ActionListener() { // 打开平台默认的浏览器 

             public void actionPerformed(ActionEvent e) { 

                 try { 

                     desktop.browse(new URI("http://blog.csdn.net/mq612")); // 打开平台默认的浏览器 

                 } catch (URISyntaxException ex) { 

                     ex.printStackTrace(); 

                 } catch (IOException ex) { 

                     ex.printStackTrace(); 

                 } 

             } 

         }); 

         button[1].addActionListener(new ActionListener() { // 打开平台默认的邮件 

             public void actionPerformed(ActionEvent e) { 

                 try { 

                     /** 

                      * 打开平台默认的邮件,有两个方法 

                      * mail() // 单独打开默认的邮件 

                      * mail(URI mailtoURI) // 带接收者地址的mail方法 

                      */

                     desktop.mail(new URI("mailto:[email protected]")); 

                 } catch (URISyntaxException ex) { 

                     ex.printStackTrace(); 

                 } catch (IOException ex) { 

                     ex.printStackTrace(); 

                 } 

             } 

         }); 

         button[2].addActionListener(new ActionListener() { // 使用平台默认程序打开文件 

             public void actionPerformed(ActionEvent e) { 

                 try { 

                     desktop.open(file); 

                 } catch (IOException ex) { 

                     ex.printStackTrace(); 

                 } 

             } 

         }); 

         button[3].addActionListener(new ActionListener() { // 使用平台默认程序编辑文件 

             public void actionPerformed(ActionEvent e) { 

                 try { 

                     desktop.edit(file); 

                 } catch (IOException ex) { 

                     ex.printStackTrace(); 

                 } 

             } 

         }); 

         button[4].addActionListener(new ActionListener() {  

         // 使用平台默认打印程序打印文件,此操作会先用默认的程序打开相应文件后再打印。 

             public void actionPerformed(ActionEvent e) { 

                 try { 

                     desktop.print(file); 

                 } catch (IOException ex) { 

                     ex.printStackTrace(); 

                 } 

             } 

         }); 

     } 

      

     /** 

      * 浏览本地文件 

      */

     private void openFile(){ 

         fc.showOpenDialog(this); 

         file = fc.getSelectedFile(); 

         text.setText(file.toString()); 

     } 

      

     public static void main(String[] args) { 

         new DesktopDemo(); 

     } 

      

 }

你可能感兴趣的:(java,浏览器,File,null,button,平台)