欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

为菜单添加透明效果,菜单添加透明效果,为菜单添加透明效果Cus

来源: javaer 分享于  点击 36079 次 点评:189

为菜单添加透明效果,菜单添加透明效果,为菜单添加透明效果Cus


为菜单添加透明效果

CustomMenuItemUI.java

import java.awt.*;import java.awt.image.*;import javax.swing.*;import javax.swing.plaf.*;import javax.swing.plaf.basic.*;public class CustomMenuItemUI extends BasicMenuItemUI {    public static ComponentUI createUI(JComponent c) {        return new CustomMenuItemUI();    }    public void paint(Graphics g, JComponent comp) {        // paint to the buffered image        BufferedImage bufimg = new BufferedImage(            comp.getWidth(),            comp.getHeight(),            BufferedImage.TYPE_INT_ARGB);        Graphics2D g2 = bufimg.createGraphics();        // restore the foreground color in case the superclass needs it        g2.setColor(g.getColor());        super.paint(g2,comp);        // do an alpha composite        Graphics2D gx = (Graphics2D) g;        gx.setComposite(AlphaComposite.getInstance(            AlphaComposite.SRC_OVER,0.8f));        gx.drawImage(bufimg,0,0,null);    }}

CustomPopupMenuUI.java

import javax.swing.JComponent;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.Popup;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.basic.BasicPopupMenuUI;public class CustomPopupMenuUI extends BasicPopupMenuUI {    public static ComponentUI createUI(JComponent c) {        return new CustomPopupMenuUI();    }    public void installUI(JComponent c) {        super.installUI(c);        popupMenu.setOpaque(false);    }    public Popup getPopup(JPopupMenu popup, int x, int y) {        Popup pp = super.getPopup(popup,x,y);        JPanel panel = (JPanel)popup.getParent();        panel.setOpaque(false);        return pp;    }}

[Java]代码

import java.awt.*;import javax.swing.*;public class Test {    public static void main(String[] args) throws Exception {         UIManager.put("PopupMenuUI","CustomPopupMenuUI");         UIManager.put("MenuItemUI","CustomMenuItemUI");        JFrame frame = new JFrame();        JMenuBar mb = new JMenuBar();        frame.setJMenuBar(mb);        JMenu menu = new JMenu("File");        mb.add(menu);        menu.add(new JMenuItem("Open"));        menu.add(new JMenuItem("Save"));        menu.add(new JMenuItem("Close"));        menu.add(new JMenuItem("Exit"));        menu = new JMenu("Edit");        mb.add(menu);        menu.add(new JMenuItem("Cut"));        menu.add(new JMenuItem("Copy"));        menu.add(new JMenuItem("Paste"));        menu.add(new JMenuItem("Paste Special.."));        frame.getContentPane().setLayout(new BorderLayout());        frame.getContentPane().add("North",new JButton("Button"));        frame.getContentPane().add("Center",new JLabel("a label"));        frame.getContentPane().add("South",new JCheckBox("checkbox"));        frame.pack();        frame.setSize(200,150);        frame.show();/*UIManager.put("MenuItemUI","CustomMenuItemUI");RepaintManager.setCurrentManager(new FullRepaintManager());*/    }}
相关栏目:

用户点评