我有以下代码来创建一个带有 PopupMenu 的 TrayIcon :

public void addToTray()
{
    try
    {
        try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }

        PopupMenu popMenu= new PopupMenu();
        MenuItem exititem = new MenuItem("Exit");
        popMenu.add(exititem);

        BufferedImage trayImg = ImageIO.read(new File("Geqo.png"));
        ImageIcon ii = new ImageIcon(trayImg);
        TrayIcon trayIcon = new TrayIcon(ii.getImage(), "Geqo", popMenu);
        trayIcon.setImageAutoSize(true);
        SystemTray.getSystemTray().add(trayIcon);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

此代码旨在创建带有 PopupMenu 的 TrayIcon。这工作正常。但我不喜欢默认的 LAF(我认为是金属)。所以我尝试将 LAF 更改为 nimbus,以及操作系统默认值 Windows,但无济于事。 LAF 似乎没有改变。谁能指导我如何更改 LAF?
提前致谢 :)!!

最佳答案

Popup 不是 Swing 组件(因此它不受 LookAndFeel 管理器的控制)。
Popup 是一个 AWT 组件,一般使用原生组件。

相反,你应该尝试更像......

public void addToTray()
{
    try
    {
        try {
            //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }

        BufferedImage trayImg = ImageIO.read(new File("Geqo.png"));
        ImageIcon ii = new ImageIcon(trayImg);
        final TrayIcon trayIcon = new TrayIcon(ii.getImage(), "Geqo", null);

        JPopupMenu jpopup = new JPopupMenu();
        JMenuItem miExit = new JMenuItem("Exit");
        jpopup.add(miExit);

        miExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                SystemTray.getSystemTray().remove(trayIcon);
                System.exit(0);
            }
        });

        trayIcon.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    jpopup.setLocation(e.getX(), e.getY());
                    jpopup.setInvoker(jpopup);
                    jpopup.setVisible(true);
                }
            }
        });

        trayIcon.setImageAutoSize(true);
        SystemTray.getSystemTray().add(trayIcon);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

这是基于 Using JPopupMneu in TrayIcon 中建议的想法

......

10-06 07:03