用户名: 密码: 验证码: 注册           网站地图 高级搜索 RSS订阅 收藏本站
山东十七地市站长联盟信息: 济南 青岛 淄博 枣庄 东营 烟台 潍坊 济宁 泰安 威海 日照 莱芜 临沂 德州 聊城 滨州 菏泽      
您的位置:主页>网络编程>Mssql>

JDK6.0新特性:Desktop和SystemTray类

[ 来源: | 作者: | 更新日期:2007-8-22 09:29:50 | 评论 0 条 | 我要投稿 ]
在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 { 字串1
    st.add(ti);
   } catch (AWTException ex) {
    ex.printStackTrace();
   }
  }
 } 字串2

 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();
  }
 }
} 字串2

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();
  }
 }
}

字串6

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();
  }
 }
} 字串6

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");
  }
 }); 字串8

 MenuItem sendMail = new MenuItem("Send Mail to me");
 sendMail.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
   sendMail("mailto:chinajash@yahoo.com.cn");
  }
 });

字串7

 MenuItem edit = new MenuItem("Edit Text File");
 sendMail.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
   edit();
  }
 }); 字串8

 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);
}

字串6

  如果在Windows中运行该程序,可以看到在系统托盘区有一个图标,右击该图标会弹出一个菜单,点击Open My Blog会打开IE,并浏览我设定的BLOG地址;点击Send Mail to me会打开Outlook Express给我发邮件;点击Edit Text File会打开记事本编辑在程序中创建的文件test.txt。

字串6


Tags:特性 new public void MenuItem static 打开 系统 ex.printStack
您的评论
用户名:新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为