| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 676 人关注过本帖
标题:关于系统托盘的问题
只看楼主 加入收藏
kd83kdi93
Rank: 2
等 级:论坛游民
帖 子:65
专家分:52
注 册:2011-9-18
结帖率:70%
收藏
 问题点数:0 回复次数:2 
关于系统托盘的问题
代码如下:
package aa;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public class A1{
    public static void main(String args[]){
        A2 a2=new A2();
    }
}

class A2 extends JFrame{
    JButton button1;
    SystemTray tray1;
    TrayIcon trayicon1;
    Image image1;
    PopupMenu popmenu1;
    MenuItem menuitem1;
    public A2(){
        this.setSize(300,300);
        this.setDefaultCloseOperation(3);
        this.setVisible(true);
        tray1=SystemTray.getSystemTray();
        image1=Toolkit.getDefaultToolkit().getImage("f:/1.jpg");
        trayicon1=new TrayIcon(image1);
        trayicon1.setToolTip("托盘实例");
        popmenu1=new PopupMenu();
        menuitem1=new MenuItem("恢复");
        popmenu1.add(menuitem1);
        trayicon1.setPopupMenu(popmenu1);
        menuitem1.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                tray1.remove(trayicon1);
               
            }
            
        });
        button1=new JButton("开始托盘");
        button1.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                try {
                    tray1.add(trayicon1);
                } catch (AWTException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
               
            }
            
        });
        trayicon1.addMouseListener(new MouseListener(){

            @Override
            public void mouseClicked(MouseEvent arg0) {
                if(arg0.getButton()==3){
                    popmenu1.show(arg0.getComponent(), arg0.getX(), arg0.getY());
                }
               
            }

            @Override
            public void mouseEntered(MouseEvent arg0) {
                // TODO Auto-generated method stub
               
            }

            @Override
            public void mouseExited(MouseEvent arg0) {
                // TODO Auto-generated method stub
               
            }

            @Override
            public void mousePressed(MouseEvent arg0) {
                // TODO Auto-generated method stub
               
            }

            @Override
            public void mouseReleased(MouseEvent arg0) {
                // TODO Auto-generated method stub
               
            }
            
        });
        this.add(button1);
    }
}

异常:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: parent is null
    at java.awt.PopupMenu.show(Unknown Source)
    at aa.A2$3.mouseClicked(A1.java:65)
    at java.awt.TrayIcon.processMouseEvent(Unknown Source)
    at java.awt.TrayIcon.processEvent(Unknown Source)
    at java.awt.TrayIcon.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: origin not in parent's hierarchy
    at java.awt.PopupMenu.show(Unknown Source)
    at aa.A2$3.mouseClicked(A1.java:65)
    at java.awt.TrayIcon.processMouseEvent(Unknown Source)
    at java.awt.TrayIcon.processEvent(Unknown Source)
    at java.awt.TrayIcon.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: origin not in parent's hierarchy
    at java.awt.PopupMenu.show(Unknown Source)
    at aa.A2$3.mouseClicked(A1.java:65)
    at java.awt.TrayIcon.processMouseEvent(Unknown Source)
    at java.awt.TrayIcon.processEvent(Unknown Source)
    at java.awt.TrayIcon.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

为什么会出异常呢?  我一般在JFrame添加弹出菜单时也是这样写的但是不知道为什么这个出错了

搜索更多相关主题的帖子: class 系统托盘 package public import 
2012-02-07 02:54
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
收藏
得分:0 
看错误信息不就知道了。
2012-02-13 09:07
懂也不懂
Rank: 4
等 级:业余侠客
威 望:2
帖 子:73
专家分:224
注 册:2010-12-1
收藏
得分:0 
是这样的,你用的这个方法arg0.getComponent()他有一个约定,就是当触发事件的对象是Componentl类型时,才返回该对象,否则返回NULL,二你这个触发事件的对象应该是TrayIcon,但是他是Object的子类,并不是Component的,所有就异常了。还有,希望以后编程的时候,尽量少用数字表示状态。。。哈哈,加油吧
2012-02-15 11:03
快速回复:关于系统托盘的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.102202 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved