當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java AWT Desktop用法及代碼示例


Desktop 類是 Java AWT 包的一部分。此類用於啟動在本機桌麵上注冊的關聯應用程序以處理URI或文件。

關於桌麵課程的要點:

  • 它可以打開顯示特定URI的默認網絡瀏覽器
  • 它可以啟動默認郵件客戶端,並帶有可選的 mailto URI
  • 它可以啟動注冊的應用程序來打開、編輯或打印特定文件。

Different Methods in Desktop Class

方法 解釋
browse(URI u) 啟動默認瀏覽器以顯示特定 URI。
edit(File f) 啟動關聯的編輯器應用程序並打開文件。
getDesktop() 返回當前瀏覽器上下文的桌麵實例。
isDesktopSupported() 返回當前平台是否支持此類。
isSupported(桌麵.Action 操作) 返回當前平台是否支持某個操作。
mail() 啟動用戶默認郵件客戶端的郵件撰寫窗口。
mail(URI mailtoURI) 啟動用戶默認郵件客戶端的郵件撰寫窗口,填充由 mailto: URI 指定的消息字段。
open(File f) 啟動關聯的應用程序以打開文件
print(File f) 使用關聯應用程序的打印命令,通過本機桌麵打印工具打印文件。

以下示例程序旨在說明 Java AWT 中的 Desktop 類:

  • 啟動瀏覽器並打開特定 URI 的程序
    
    // Java Program to Launch the browser 
    // and open a specific URI 
    import java.awt.*; 
    import javax.swing.*; 
    import java.awt.event.*; 
    import java.net.*; 
      
    class desk extends JFrame implements ActionListener { 
      
        // frame 
        static JFrame f; 
          
        // Main Method  
        public static void main(String args[]) 
        { 
            desk d = new desk(); 
      
            // create a frame 
            f = new JFrame("desktop"); 
      
            // create a panel 
            JPanel p = new JPanel(); 
      
            // create a button 
            JButton b = new JButton("launch"); 
      
            // add Action Listener 
            b.addActionListener(d); 
      
            p.add(b); 
            f.add(p); 
            f.show(); 
            f.setSize(200, 200); 
        } 
      
        // if button is pressed 
        public void actionPerformed(ActionEvent e) 
        { 
            try { 
      
                // create a URI 
                URI u = new URI("www.geeksforgeeks.org"); 
      
                Desktop d = Desktop.getDesktop(); 
                d.browse(u); 
            } 
            catch (Exception evt) { 
            } 
        } 
    } 

    輸出:

  • 用於將郵件發送到特定 URI 的程序
    
    // Java Program to Launch the 
    // mail to a specific URI 
    import java.awt.*; 
    import javax.swing.*; 
    import java.awt.event.*; 
    import java.net.*; 
      
    class desk extends JFrame implements ActionListener { 
      
        // frame 
        static JFrame f; 
      
        // Main Method 
        public static void main(String args[]) 
        { 
            desk d = new desk(); 
      
            // create a frame 
            f = new JFrame("desktop"); 
      
            // create a panel 
            JPanel p = new JPanel(); 
      
            // create a button 
            JButton b = new JButton("launch"); 
      
            // add Action Listener 
            b.addActionListener(d); 
      
            p.add(b); 
            f.add(p); 
            f.show(); 
            f.setSize(200, 200); 
        } 
      
        // if button is pressed 
        public void actionPerformed(ActionEvent e) 
        { 
            try { 
                URI u = new URI("mailto:review-team@geeksforgeeks.org"); 
      
                Desktop d = Desktop.getDesktop(); 
                d.mail(u); 
            } 
            catch (Exception evt) { 
                JOptionPane.showMessageDialog(this, evt.getMessage()); 
            } 
        } 
    } 

    輸出:

  • 打開文件的程序
    
    // Java Program to open a file 
    import java.awt.*; 
    import javax.swing.*; 
    import java.awt.event.*; 
    import java.io.*; 
    import java.net.*; 
    class desk extends JFrame implements ActionListener { 
        // frame 
        static JFrame f; 
           
        // Main Method 
        public static void main(String args[]) 
        { 
            desk d = new desk(); 
      
            // create a frame 
            f = new JFrame("desktop"); 
      
            // create a panel 
            JPanel p = new JPanel(); 
      
            // create a button 
            JButton b = new JButton("launch"); 
      
            // add Action Listener 
            b.addActionListener(d); 
      
            p.add(b); 
            f.add(p); 
            f.show(); 
            f.setSize(200, 200); 
        } 
      
        // if button is pressed 
        public void actionPerformed(ActionEvent e) 
        { 
            try { 
      
                // create a file 
                File u = new File("f:\\image.png"); 
      
                Desktop d = Desktop.getDesktop(); 
                d.open(u); 
            } 
            catch (Exception evt) { 
                JOptionPane.showMessageDialog(this, evt.getMessage()); 
            } 
        } 
    } 

    輸出:

  • 打開文件進行編輯的程序
    
    // Java Program to open a file for editing 
    import java.awt.*; 
    import javax.swing.*; 
    import java.awt.event.*; 
    import java.io.*; 
    import java.net.*; 
      
    class desk extends JFrame implements ActionListener { 
      
        // frame 
        static JFrame f; 
      
        // Main Method 
        public static void main(String args[]) 
        { 
            desk d = new desk(); 
      
            // create a frame 
            f = new JFrame("desktop"); 
      
            // create a panel 
            JPanel p = new JPanel(); 
      
            // create a button 
            JButton b = new JButton("launch"); 
      
            // add Action Listener 
            b.addActionListener(d); 
      
            p.add(b); 
            f.add(p); 
            f.show(); 
            f.setSize(200, 200); 
        } 
      
        // if button is pressed 
        public void actionPerformed(ActionEvent e) 
        { 
            try { 
      
                // create a file 
                File u = new File("f:\\sample.txt"); 
      
                Desktop d = Desktop.getDesktop(); 
                d.edit(u); 
            } 
            catch (Exception evt) { 
                JOptionPane.showMessageDialog(this, evt.getMessage()); 
            } 
        } 
    } 

    輸出:

  • 打開文件進行打印的程序
    
    // Java Program to open a file for printing 
    import java.awt.*; 
    import javax.swing.*; 
    import java.awt.event.*; 
    import java.io.*; 
    import java.net.*; 
      
    class desk extends JFrame implements ActionListener { 
      
        // frame 
        static JFrame f; 
      
        // Main Method 
        public static void main(String args[]) 
        { 
            desk d = new desk(); 
      
            // create a frame 
            f = new JFrame("desktop"); 
      
            // create a panel 
            JPanel p = new JPanel(); 
      
            // create a button 
            JButton b = new JButton("launch"); 
      
            // add Action Listener 
            b.addActionListener(d); 
      
            p.add(b); 
            f.add(p); 
            f.show(); 
            f.setSize(200, 200); 
        } 
      
        // if button is pressed 
        public void actionPerformed(ActionEvent e) 
        { 
            try { 
      
                // create a file 
                File u = new File("f:\\sample.txt"); 
      
                Desktop d = Desktop.getDesktop(); 
                d.print(u); 
            } 
            catch (Exception evt) { 
                JOptionPane.showMessageDialog(this, evt.getMessage()); 
            } 
        } 
    } 

    輸出:

參考: https://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html



相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 Java AWT | Desktop Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。