java.awt下systemtray支持的功能

pu3pd22g  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(345)

我做了一个简单的java程序来显示任务栏中的托盘图标,具有多种功能。现在代码在每次启动时显示图标是不一致的,所以我想对代码使用不同的导入,并用java.awt替换dorkbox.systemtray。现在java.awt有systemtray函数,但是dorkbox下提供了许多java.awt下不支持的函数,比如setimage、setstatus和getmenu。这些函数是否有相应的函数?
这是我的密码:

package test2;

import java.awt.*;
import java.awt.Desktop;
import java.awt.MenuItem;
import java.awt.SystemTray;

import javax.swing.JCheckBoxMenuItem;

import java.awt.event.ActionEvent;

/**
 *
 * @author ioss
 */

import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

//import dorkbox.systemTray.Checkbox;
//import dorkbox.systemTray.Entry;
//import dorkbox.systemTray.Menu;
//import dorkbox.systemTray.MenuItem;
//import dorkbox.systemTray.Separator;
//import dorkbox.systemTray.SystemTray;
//import dorkbox.systemTray.SystemTray.TrayType;
//import dorkbox.systemTray.ui.swing.SwingUIFactory;
//import dorkbox.systemTray.*;

public class Tray { 

    public static String email,admin,olduser;
    public static SystemTray systemTray;
    public static MenuItem on, of; 

    public static void main(String[] args) {

   systemTray=SystemTray.getSystemTray(); 
   System.out.println("systemTray is "+systemTray);
   showTray();
    }

    public static void showTray()
    {
        if (systemTray==null)
        {           
            System.exit(0); 
        }
        //String[] args= {"",""};

        // ImageIcon icon = new ImageIcon("/home/ioss/Downloads/hexagon-clipart.jpeg");
        systemTray.setImage("/home/ioss/Downloads/c4f9.png"); 
        systemTray.setStatus("Offline"); 

ActionListener online = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {             
                systemTray.setImage("/home/ioss/Downloads/c4f9.png"); 
                on.setEnabled(false);
                of.setEnabled(true);
                System.out.println("online..");
                systemTray.setStatus("online");                 
            }

        };

        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"); 

            }

        };

    PopupMenu pm=new PopupMenu();   
        of =new MenuItem("Go Offline");
    pm.add(of);

    on =new MenuItem("Go Online");
    pm.add(on);

//      on =new MenuItem("Go Online",online);

        //My Desklog

        ActionListener myDesklog =new ActionListener()
                {

                    @Override
                    public void actionPerformed(ActionEvent arg0) {

                        try {
                            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) {

                 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);

            }

        };

        MenuItem link =new MenuItem ("My Desklog"); 
        MenuItem chgImg=new MenuItem("Change Icon"); 
        // MenuItem user_email=new MenuItem(email);
            MenuItem version=new MenuItem("Version-1.0.4");
            of.setEnabled(false);
            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');
            }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题