国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

JDK6.0新特性:Desktop和SystemTray類

2019-11-18 15:29:40
字體:
來源:轉載
供稿:網友

  在JDK6中 ,AWT新增加了兩個類:Desktop和SystemTray,前者可以用來打開系統默認瀏覽器瀏覽指定的URL,打開系統默認郵件客戶端給指定的郵箱發郵件,用默認應用程序打開或編輯文件(比如,用記事本打開以txt為后綴名的文件),用系統默認的打印機打印文檔;后者可以用來在系統托盤區創建一個托盤程序。下面代碼演示了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()){//判定當前平臺是否支持Desktop類
   desktop = Desktop.getDesktop();
  }
  if(SystemTray.isSupported()){//判定當前平臺是否支持系統托盤
   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("http://blog.csdn.net/chinajash");
  }
 });

 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);
}
}
  假如在Windows中運行該程序,可以看到在系統托盤區有一個圖標,右擊該圖標會彈出一個菜單,點擊Open My Blog會打開IE,并瀏覽我設定的BLOG地址;點擊Send Mail to me會打開Outlook EXPress給我發郵件;點擊Edit Text File會打開記事本編輯在程序中創建的文件test.txt。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 仙居县| 江城| 清河县| 晋城| 亚东县| 成安县| 浦县| 三河市| 陆川县| 大悟县| 腾冲县| 讷河市| 四平市| 洞头县| 西青区| 六盘水市| 凌海市| 镇安县| 福建省| 佳木斯市| 沁水县| 云南省| 汉阴县| 安图县| 凤山县| 泰兴市| 新邵县| 北流市| 民权县| 英山县| 闽清县| 满洲里市| 二连浩特市| 上虞市| 塔城市| 邹城市| 东阳市| 南丰县| 晴隆县| 兴安县| 靖安县|