7

如何在任务栏中的应用程序的弹出菜单中添加垂直分隔符?

   tray = SystemTray.getSystemTray();             
   openMenuItem = new MenuItem("Open P");
   stopKLMenuItem = new MenuItem("Stop");
   exitMenuItem = new MenuItem("exit");
   menu.add(exitMenuItem);
   menu.add(stopKLMenuItem);
   menu.addSeparator(); // adds a horizontal separator
   menu.add(openMenuItem);
   trayIcon = new TrayIcon(image,"P",menu);

该语句menu.addSeparator()添加了一个水平分隔符。我还想要一个垂直分隔符,例如:

在此处输入图像描述

我该怎么做呢 ?

4

2 回答 2

6

一个简单的技巧是向 JMenuItem 添加一个空图标。外观然后添加您正在寻找的垂直分隔符(当然,这仅适用于 Windows L&F,其他 L&F 可能会以不同方式呈现):

菜单示例结果

以及产生它的代码:

import java.awt.AWTException;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTrayIcon {

    protected void initUI() {

        TrayIcon trayIcon = new TrayIcon(new ImageIcon(
                "http://3.bp.blogspot.com/-nh7fv5FqpU4/TeUbTvAdSkI/AAAAAAAAAUo/Ig53KJGvzlk/s45/avatar.png").getImage());

        final JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem checkBox1 = new JMenuItem("Last checked...", new ImageIcon(new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB)));
        JMenuItem checkBox2 = new JMenuItem("Open...");

        popupMenu.add(checkBox1);
        popupMenu.addSeparator();
        popupMenu.add(checkBox2);

        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popupMenu.setLocation(e.getX(), e.getY());
                    popupMenu.setInvoker(popupMenu);
                    popupMenu.setVisible(true);
                }
            }
        });
        try {
            SystemTray.getSystemTray().add(trayIcon);
        } catch (AWTException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (UnsupportedLookAndFeelException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                new TestTrayIcon().initUI();
            }
        });
    }

}
于 2012-09-28T12:55:31.310 回答
1

想法

  1. 添加BordersMatteBorders例如)到JMenuItem(s)

  2. 添加JPanelJPopup

    • 必须设置BorderLayoutJPopup放入JPanel( CENTER areaand/or with Borderstoo)

    • 设置BorderLayoutJPanel

    • 放在JSeparator或区域(取决于或方向EASTWEST

    • JPanel放另一个JButtons模拟JMenuItem(第一眼没有差异)输出到Swing ActionorActionListener

  3. JWindow(我默认使用JPopupToolTip)相同的东西而不是JPopup

  4. 也许我错了,但JPopup看起来像变色龙

    • 一次JPanel

    • 另一个作为JWindow

    • 抱歉没有最深入的了解,感兴趣

于 2012-09-28T11:41:10.103 回答