溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何分析JDK6.0的Desktop和SystemTray類特性

發(fā)布時間:2021-12-03 10:06:16 來源:億速云 閱讀:142 作者:柒染 欄目:編程語言

今天就跟大家聊聊有關(guān)如何分析JDK6.0的Desktop和SystemTray類特性,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

在JDK6中 ,AWT新增加了兩個類:Desktop和SystemTray,前者可以用來打開系統(tǒng)默認瀏覽器瀏覽指定的URL,打開系統(tǒng)默認郵件客戶端給指定的郵箱發(fā)郵件,用默認應(yīng)用程序打開或編輯文件(比如,用記事本打開以txt為后綴名的文件),用系統(tǒng)默認的打印機打印文檔;后者可以用來在系統(tǒng)托盤區(qū)創(chuàng)建一個托盤程序.下面代碼演示了Desktop和SystemTray的用法.

/**
*
* @author chinajash
*/
public class DesktopTray {
   private static Desktop desktop;
   private static SystemTray st;
   private static PopupMenu pm;
   public static void main(String[] args) {
       if(Desktop.isDesktopSupported()){//判斷當(dāng)前平臺是否支持Desktop類
           desktop = Desktop.getDesktop();
       }
       if(SystemTray.isSupported()){//判斷當(dāng)前平臺是否支持系統(tǒng)托盤
           st = SystemTray.getSystemTray();
           Image image = Toolkit.getDefaultToolkit().getImage("netbeans.png");//定義托盤圖標的圖片            
           createPopupMenu();
           TrayIcon ti = new TrayIcon(image, "Desktop Demo Tray", pm);
           try {
               st.add(ti);
           } catch (AWTException ex) {
               ex.printStackTrace();
           }
       }
   }
   
   public static void sendMail(String mail){
       if(desktop!=null && desktop.isSupported(Desktop.Action.MAIL)){
           try {
               desktop.mail(new URI(mail));
           } catch (IOException ex) {
               ex.printStackTrace();
           } catch (URISyntaxException ex) {
               ex.printStackTrace();
           }
       }            
   }
   
   public static void  openBrowser(String url){
       if(desktop!=null && desktop.isSupported(Desktop.Action.BROWSE)){
           try {
               desktop.browse(new URI(url));
           } catch (IOException ex) {
               ex.printStackTrace();
           } catch (URISyntaxException ex) {
               ex.printStackTrace();
           }
       }
   }
   
   public static void  edit(){
       if(desktop!=null && desktop.isSupported(Desktop.Action.EDIT)){
           try {
               File txtFile = new File("test.txt");
               if(!txtFile.exists()){
                   txtFile.createNewFile();
               }
               desktop.edit(txtFile);
           } catch (IOException ex) {
               ex.printStackTrace();
           }
       }
   }
   
   public static void createPopupMenu(){
      pm = new PopupMenu();
       MenuItem openBrowser = new MenuItem("Open My Blog");
       openBrowser.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               openBrowser("
");
           }
       });
       
       MenuItem sendMail = new MenuItem("Send Mail to me");
       sendMail.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               sendMail("
mailto:chinajash@yahoo.com.cn");
           }
       });
       
       MenuItem edit = new MenuItem("Edit Text File");
       sendMail.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               edit();
           }
       });
       
       MenuItem exitMenu = new MenuItem("&Exit");
       exitMenu.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               System.exit(0);
           }
       });
       pm.add(openBrowser);
       pm.add(sendMail);
       pm.add(edit);
       pm.addSeparator();
       pm.add(exitMenu);    
   }
}
http://blog.csdn.net/chinajash

如果在Windows中運行該程序,可以看到在系統(tǒng)托盤區(qū)有一個圖標,右擊該圖標會彈出一個菜單,點擊Open My Blog會打開IE,并瀏覽"http://blog.csdn.net/chinajash";點擊Send Mail to me會打開Outlook Express給我發(fā)郵件;點擊Edit Text File會打開記事本編輯在程序中創(chuàng)建的文件test.txt

看完上述內(nèi)容,你們對如何分析JDK6.0的Desktop和SystemTray類特性有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI