繁体   English   中英

无法使 TrayIcon 中的 MenuItem 可点击

[英]Unable to make the MenuItem in TrayIcon clickable

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

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

就我而言,我已经包含了许多选项,但是没有一个 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
                
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM