Java6實現調用操作平臺桌面系統

d6nd 9年前發布 | 6K 次閱讀 Java

import java.awt.Desktop;
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 <a href="http://blog.csdn.net/mq612">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("<a >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:mq612@163.com"));
           } 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(); }

}</pre>
Java1.6.0實現調用操作平臺桌面系統 Desktop類將獲得操作平臺的桌面系統,以便使用系統默認瀏覽器、編輯器、郵件、打印等 一堆按鈕擺在一起不大好看,懶的布局了,大家能看明白就成,打開文件、編輯文件和打印文件需要先按“瀏覽”按鈕

 本文由用戶 d6nd 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!