本文介绍了具有系统任务栏上下文菜单的渐变背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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..

先谢谢了.

推荐答案

hmmm仅此博客由SwingGuru的其中一位可以帮助您

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

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

这篇关于具有系统任务栏上下文菜单的渐变背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 14:09