Having gradient background of system tray context

2019-07-16 04:10发布

I am developing desktop application in java.On start up my application runs from system tray.But the system tray's context menu is having standard look and feel according to OS.I want to customize the context menu.I want to give gradient background to it , want to change fonts,borders etc.Let me know is this possible ? If there are some examples for that please provide links for the same..

Thanks in advance..

1条回答
甜甜的少女心
2楼-- · 2019-07-16 04:36

hmmm only this blog by one of SwingGuru's can hepl you with that

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

and

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());
    }
}
查看更多
登录 后发表回答