无法使 TrayIcon 中的 MenuItem 可点击

Unable to make the MenuItem in TrayIcon clickable

我制作了一个简单的 java 程序来显示一个系统托盘,其中包含许多菜单项,单击它就会出现。所有这些菜单项都可以点击。

我在 ActionListener 中包含了托盘图标功能的代码。

我已经包含了很多选项,但是 none 的 ActionListener 正在工作。

程序没有抛出任何错误,但我无法为按钮添加更多功能。

这是我的代码:


    
    public static void showTray()
    {
        if (systemTray==null)
        {           
            System.exit(0); 
        }
        else
        {
            System.out.println("SystemTray works"); 
        }
        //String[] args= {"",""};
          
        // ImageIcon icon = new ImageIcon("/home/ioss/Downloads/hexagon-clipart.jpeg");
        systemTray.setImage("/home/ioss/Downloads/c4f9.png"); 
        systemTray.setStatus("Running"); 
        

        // MenuItem user_email=new MenuItem(email);
        MenuItem chgImg=new MenuItem("Change Icon");    
        MenuItem link =new MenuItem ("My Desklog"); 
            MenuItem version=new MenuItem("Version-1.0.4");
           
            systemTray.getMenu().add(link).setShortcut('q');
            systemTray.getMenu().add(version).setShortcut('q');
            
          
           
//          systemTray.getMenu().add(of).setShortcut('q');
//          systemTray.getMenu().add(on).setShortcut('q');
//          systemTray.getMenu().add(logout1).setShortcut('q');
            systemTray.getMenu().add(chgImg).setShortcut('q');
        
        PopupMenu pm=new PopupMenu();   
        
        op=new MenuItem("About"); 
          systemTray.getMenu().add(op).setShortcut('q');
        
        on =new MenuItem("Go Online");
         systemTray.getMenu().add(on).setShortcut('q');
         on.setEnabled(false);
        
ActionListener online = new ActionListener() {

            
            @Override
            public void actionPerformed(ActionEvent arg0) { 
                System.out.println("Inside Online Listener"); 
                systemTray.setImage("/home/ioss/Downloads/c4f9.png"); 
                    
                    System.out.println("On enabled");
                    
                of.setEnabled(true);
                System.out.println("online..");
                systemTray.setStatus("online");                 
            }
            
        };
        
        of =new MenuItem("Go Offline");
        
        
        ActionListener offline = new ActionListener() {

            
            @Override
            public void actionPerformed(ActionEvent arg0) {
                
                systemTray.setImage("/home/ioss/Downloads/prohibition-symbol.jpeg"); 
                on.setEnabled(true);
                of.setEnabled(false);
                System.out.println("offline..");
                systemTray.setStatus("offline"); 
                
            }
            
        };
          systemTray.getMenu().add(of).setShortcut('q');
        
//  PopupMenu pm=new PopupMenu();   
//      of =new MenuItem("Go Offline");
//      op=new MenuItem("About"); 
        
//  pm.add(of);
    
    
//  pm.add(on);
    
//      on =new MenuItem("Go Online",online);
        
        //My Desklog
    
        
        ActionListener myDesklog =new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        
                        
                    
                        try {
                            System.out.println("Desklog Listener"); 
//                          Desktop.getDesktop().browse(new URI("https://app.desklog.io/user/home#"));
                            
                            String url_open ="http://javadl.sun.com/webapps/download/AutoDL?BundleId=76860";
//                          java.awt.Desktop.getDesktop().browse(java.net.URI.create(url_open));
                            
                            java.awt.Desktop.getDesktop().browse(new URI("https://app.desklog.io/user/home#"));
                            
                            
                        } catch (IOException e) {
                            
                            e.printStackTrace();
                        } catch (URISyntaxException e) {
                            
                            e.printStackTrace();
                        }
                        
                    }
            
                };
                //LOGOUT 
        MenuItem logout =new MenuItem("Logout");
        
          ActionListener logout1 =new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    System.exit(0);
                    
                }
        
            }; 
        
                
        
        
        ///////////////Change Icon
        
ActionListener ChangeIcon = new ActionListener() {


            @Override
            public void actionPerformed(ActionEvent arg0) {
                
                System.out.println("Inside ActionLisener"); 
                 JButton button1 = new JButton("One");
                 JButton button2 = new JButton("Two");
                  
                 
                  
                  Icon icon = new ImageIcon("/home/ioss/Downloads/hexagon-clipart.jpeg");
                  JButton button3 = new JButton(icon);
                  Box box = Box.createVerticalBox();
                  box.add(button1);
                  box.add(button2);
                  box.add(button3);
                 
                
                  JFrame frame = new JFrame();
                  frame.add(box);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setLocationByPlatform(true);
                  frame.setSize(500, 300);
                  frame.setVisible(true);
                
            }
            
        };
    
    }
    }



尝试在 actonListener 中添加一个 implements 方法,它不一定需要主体。像这样:

public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                
            }