0

有没有办法在 PopupMenu(使用 TrayIcon)中渲染像 JPanel 这样的图形?我知道使用 JPopupMenu 是可能的,但我不喜欢在弹出窗口外部单击时不会关闭(并且图标不会像 PopupMenu 那样突出显示)。

我试图用 JPopupMenu 做的例子:

    if( SystemTray.isSupported() ) {
        // Get the SystemTray instance
        SystemTray tray = SystemTray.getSystemTray();

        // Load icon
        Image image = new ImageIcon(this.getClass().getResource("delete.png")).getImage();

        final JPopupMenu popup = new JPopupMenu();
        popup.add( new JMenuItem("Test") );

        JPanel p1 = new JPanel();
        p1.setBackground( Color.red );
        p1.setPreferredSize( new Dimension(200, 300) );
        popup.add( p1 );

        JTrayIcon trayIcon = new JTrayIcon( image );
        trayIcon.setJPopupMenu( popup );

        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                popup.setLocation(e.getX(), e.getY());
                popup.setInvoker(popup);
                popup.setVisible(true);
            }
        });

        try {
            tray.add( trayIcon );
        } catch (Exception e) {
            JOptionPane.showMessageDialog( null, "Could not add tray icon." );
        }
    }
4

2 回答 2

1

有没有办法在 PopupMenu 中渲染像 JPanel 这样的图形?我知道使用 JPopupMenu 是可能的,但我不喜欢在弹出窗口外部单击时不会关闭(并且图标不会像 PopupMenu 那样突出显示)。

  • 我只会Java-2D直接谈论弹出容器,确保自定义绘画没有问题JPanel,用JButtons, 铺设GridLayout

  • 是的,有几种方法,@Kirill Grouchnikov 的最佳描述

  • 你可以决定是否创建

    1) 每个JPopupMenu/的新油漆JMenu

    2)放入UIManager(然后对所有Objects当前有效JVM

于 2012-12-20T10:52:36.427 回答
0
You can extending JPopupMenu and add customItem to it:
public class CustomPopUp extends JPopupMenu {

    public CustomPopUp() {
        reload();
    }

    private void reload(final Collection<CustomItem> items) throws BadLocationException {
        for (final CustomItem item : items) {
            add(new AbstractAction(item.getLabel(), item.getIcon()) {               
                @Override
                public void actionPerformed(final ActionEvent e) {
                    //do whatever
                }
            });
        }

    }
}
public class CustomItem {
    private String label;
    private ImageIcon icon;

    //getter and setter
}
于 2012-12-20T11:22:35.970 回答