[转]JDK1.6.0新特性详解与代码示例

JDK6.0发布有段时间了,新的JDK也有不少新的特性,我去网上搜集了一下,列在下面和大家一起学习.
1.Desktop和SystemTray. 在JDK6中 ,AWT新增加了两个类:Desktop和SystemTray,前者可以用来打开系统默认浏览器浏览指定的URL,打开系统默认邮件客户端给指定的邮箱发邮件,用默认应用程序打开或编辑文件(比如,用记事本打开以txt为后缀名的文件),用系统默认的打印机打印文档;后者可以用来在系统托盘区创建一个托盘程序。

我随便找了几张图,在Tray里面都是空的,没有图,可能是图太大,有xdjm知道希望告诉我

None.gif import  java.awt.AWTException;
None.gif
import  java.awt.Desktop;
None.gif
import  java.awt.Image;
None.gif
import  java.awt.MenuItem;
None.gif
import  java.awt.PopupMenu;
None.gif
import  java.awt.SystemTray;
None.gif
import  java.awt.Toolkit;
None.gif
import  java.awt.TrayIcon;
None.gif
import  java.awt.event.ActionEvent;
None.gif
import  java.awt.event.ActionListener;
None.gif
import  java.io.File;
None.gif
import  java.io.IOException;
None.gif
import  java.net.URI;
None.gif
import  java.net.URISyntaxException;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  DesktopTrayTest dot.gif dot.gif {
InBlock.gif    
private   static  Desktop desktop;
InBlock.gif    
private   static  SystemTray st;
InBlock.gif    
private   static  PopupMenu pm;
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static   void  main( String[] args ) dot.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if ( Desktop.isDesktopSupported() ) dot.gif dot.gif {
InBlock.gif            desktop 
=  Desktop.getDesktop();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if ( SystemTray.isSupported() ) dot.gif dot.gif {
InBlock.gif            st 
=  SystemTray.getSystemTray();
InBlock.gif            Image image 
=  Toolkit.getDefaultToolkit().createImage(  " http://www.51ppt.com.cn/Article/Uploadphotos/200604/20064147333288.png "  );
InBlock.gif            createPopupMenu();
InBlock.gif            TrayIcon ti 
=   new  TrayIcon( image,  " Demo " , pm );
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try dot.gif dot.gif {
InBlock.gif                st.add( ti );
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
  catch ( AWTException awte ) dot.gif dot.gif {
InBlock.gif                awte.printStackTrace();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static   void  sendMail( String mail ) dot.gif dot.gif {
InBlock.gif        
if ( desktop  !=   null   &&
ExpandedSubBlockStart.gifContractedSubBlock.gif            desktop.isSupported( Desktop.Action.MAIL ) ) dot.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try  dot.gif dot.gif {
InBlock.gif                desktop.mail( 
new  URI( mail ) );
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
  catch  (IOException e) dot.gif dot.gif {
InBlock.gif                e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
  catch  (URISyntaxException e) dot.gif dot.gif {
InBlock.gif                e.printStackTrace();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static   void  openBrowser( String url ) dot.gif dot.gif {
InBlock.gif        
if ( desktop  !=   null   &&
ExpandedSubBlockStart.gifContractedSubBlock.gif            desktop.isSupported( Desktop.Action.BROWSE )) dot.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try  dot.gif dot.gif {
InBlock.gif                desktop.browse( 
new  URI( url ) );
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
  catch  (IOException e) dot.gif dot.gif {
InBlock.gif                e.printStackTrace();
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
  catch  (URISyntaxException e) dot.gif dot.gif {
InBlock.gif                e.printStackTrace();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static   void  edit() dot.gif dot.gif {
InBlock.gif        
if ( desktop  !=   null   &&
ExpandedSubBlockStart.gifContractedSubBlock.gif            desktop.isSupported( Desktop.Action.EDIT ) ) dot.gif
dot.gif {
InBlock.gif            File file 
=   new  File(  " test.txt "  );
ExpandedSubBlockStart.gifContractedSubBlock.gif            
try  dot.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if ( file.exists()  ==   false  ) dot.gif dot.gif {
InBlock.gif                    file.createNewFile();
ExpandedSubBlockEnd.gif                }

InBlock.gif                desktop.edit( file );
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
  catch ( IOException ioe ) dot.gif dot.gif {
InBlock.gif                ioe.printStackTrace();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static   void  createPopupMenu() dot.gif dot.gif {
InBlock.gif        pm 
=   new  PopupMenu();
InBlock.gif        MenuItem ob 
=   new  MenuItem(  " Open url "  );
ExpandedSubBlockStart.gifContractedSubBlock.gif        ob.addActionListener( 
new  ActionListener() dot.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public   void  actionPerformed( ActionEvent ae ) dot.gif dot.gif {
InBlock.gif                openBrowser( 
" http://blog.csdn.net/xumingming64398966 "  );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
);
InBlock.gif        MenuItem sm 
=   new  MenuItem(  " Send Mail "  );
ExpandedSubBlockStart.gifContractedSubBlock.gif        sm.addActionListener( 
new  ActionListener() dot.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public   void  actionPerformed( ActionEvent ae ) dot.gif dot.gif {
InBlock.gif                sendMail( 
" 64398966@qq.com "  );
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
);
InBlock.gif        MenuItem ed 
=   new  MenuItem(  " Edit "  );
ExpandedSubBlockStart.gifContractedSubBlock.gif        ed.addActionListener( 
new  ActionListener() dot.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif            
public   void  actionPerformed( ActionEvent ae ) dot.gif dot.gif {
InBlock.gif                edit();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
);
InBlock.gif        MenuItem ex 
=   new  MenuItem(  " Exit "  );
ExpandedSubBlockStart.gifContractedSubBlock.gif        ex.addActionListener( 
new  ActionListener() dot.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值