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

发布于 2024-11-18 04:45:33 字数 150 浏览 1 评论 0原文

我正在用java开发桌面应用程序。启动时我的应用程序从系统托盘运行。但是系统托盘的上下文菜单根据操作系统具有标准的外观和感觉。我想自定义上下文菜单。我想为其提供渐变背景,想要更改字体、边框等。请告诉我这可能吗? 如果有一些示例,请提供相同的链接..

提前致谢..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2024-11-25 04:45:33

嗯,只有这个博客,作者之一SwingGuru 可以帮助您,

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

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());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文