在任务栏的PopupMenu中添加垂直分隔符 [英] Adding a vertical separator in PopupMenu, in the task bar

查看:224
本文介绍了在任务栏的PopupMenu中添加垂直分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I add a vertical separator in the pop up menu of the app in the task bar ?

   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()添加一个水平分隔符。我还想要一个垂直分隔符,如:

The statement menu.addSeparator() adds a horizontal separator. I also wanted a vertical separator like :

我该怎么做?

推荐答案

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

One easy trick is to add an empty icon to the JMenuItem. The look and feel then adds the vertical separator you are looking for (of course, this only works with the Windows L&F, other L&F may render this differently):

以及代码产生它:

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

}

这篇关于在任务栏的PopupMenu中添加垂直分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆