具有系统托盘上下文菜单的渐变背景

时间:2011-07-04 17:22:35

标签: java swing gradient system-tray

我正在java中开发桌面应用程序。启动我的应用程序从系统托盘运行。但系统托盘的上下文菜单根据操作系统具有标准外观。我想自定义上下文菜单。我想给出渐变它的背景,想要改变字体,边框等让我知道这可能吗?    如果有一些例子,请提供相同的链接..

提前致谢..

1 个答案:

答案 0 :(得分:2)

只有SwingGuru的一个blog可以帮助你解决这个问题

import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class FunnyMenuTest {

    private JPopupMenu popupMenu;
    private JMenuItem m_mniInsertRow;
    private JMenuItem m_mniInsertScrip;
    private JMenuItem m_mniDeleterRow;
    private JMenuItem m_mniDeleteExpiredScrip;
    private JMenuItem m_mniSetAlert;

    public void makeUI() {
        JFrame frame = new JFrame("Funny JMenu Test");
        popupMenu = new JPopupMenu();
        m_mniInsertRow = new JMenuItem("Insert a Row");
        m_mniInsertRow.setOpaque(false);
        m_mniInsertScrip = new JMenuItem("Insert a Scrip");
        m_mniInsertScrip.setOpaque(false);
        m_mniDeleterRow = new JMenuItem("Delete a Row");
        m_mniDeleterRow.setOpaque(false);
        m_mniDeleteExpiredScrip = new JMenuItem("Delete a Expired Scrip");
        m_mniDeleteExpiredScrip.setOpaque(false);
        m_mniSetAlert = new JMenuItem("Set Alert");
        m_mniSetAlert.setOpaque(false);
        popupMenu.add(m_mniInsertRow);
        popupMenu.add(m_mniInsertScrip);
        popupMenu.addSeparator();
        popupMenu.add(m_mniDeleterRow);
        popupMenu.add(m_mniDeleteExpiredScrip);
        popupMenu.add(new JSeparator());
        popupMenu.add(m_mniSetAlert);
        JMenuBar menuBar = new JMenuBar();
        for (int i = 0; i < 5; i++) {
            JMenu menu = new JMenu("Menu " + i);
            for (int j = 0; j < 5; j++) {
                JMenuItem item = new JMenuItem("Item " + j);
                item.setOpaque(false);
                menu.add(item);
            }
            menuBar.add(menu);
        }
        JMenu menu = new JMenu("Menu 1");
        JMenuItem item = new JMenuItem("Item ");
        item.setOpaque(false);
        menu.add(item);
        menuBar.add(menu);
        JPanel jp = new JPanel();
        jp.setPreferredSize(new Dimension(400, 400));
        jp.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    int x = e.getX();
                    int y = e.getY();
                    popupMenu.show(e.getComponent(), x, y);
                }
            }
        });

        frame.getContentPane().add(jp);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(400, 400));
        frame.setLocationRelativeTo(null);
        frame.setJMenuBar(menuBar);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FunnyMenuTest().makeUI();
            }
        });
        UIManager.put("PopupMenuUI", "JMenu.GradientPopupMenuUI");
    }
}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.Paint;
import java.awt.geom.Point2D;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicPopupMenuUI;

public class GradientPopupMenuUI extends BasicPopupMenuUI {

    private Paint paint = new LinearGradientPaint(new Point2D.Double(5, -5), new Point2D.Double(),
            new float[]{0.2F, 0.9F}, new Color[]{Color.WHITE, Color.GREEN}, CycleMethod.REFLECT);

    public static ComponentUI createUI(JComponent c) {
        return new GradientPopupMenuUI();
    }

    @Override
    public void paint(Graphics g, JComponent c) {
        ((Graphics2D) g).setPaint(paint);
        g.fillRect(0, 0, c.getWidth(), c.getHeight());
    }
}