服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java中的两个类:desktop和systemtray

在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,并浏览"http://blog.csdn.net/chinajash";点击send mail to me会打开outlook express给我发邮件;点击edit text file会打开记事本编辑在程序中创建的文件test.txt


扫描关注微信公众号