关于系统托盘的问题
代码如下: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添加弹出菜单时也是这样写的但是不知道为什么这个出错了
----------------解决方案--------------------------------------------------------
看错误信息不就知道了。
----------------解决方案--------------------------------------------------------
是这样的,你用的这个方法arg0.getComponent()他有一个约定,就是当触发事件的对象是Componentl类型时,才返回该对象,否则返回NULL,二你这个触发事件的对象应该是TrayIcon,但是他是Object的子类,并不是Component的,所有就异常了。还有,希望以后编程的时候,尽量少用数字表示状态。。。哈哈,加油吧
----------------解决方案--------------------------------------------------------