日期:2014-05-20  浏览次数:20763 次

请版主教我怎么重绘popupmenu
右键菜单显示一个图片,上面是我的选项,
不知道怎么实现

------解决方案--------------------
引用:
@huxiweng,重绘按钮啊,panel都可以,这个没搞出来,这个不搞出来,主题风格就很不协调了


你先把你实现那部分代码发一下

------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:

@huxiweng,重绘按钮啊,panel都可以,这个没搞出来,这个不搞出来,主题风格就很不协调了


你先把你实现那部分代码发一下



这是在桌面显示的一个动态label

右键是这样的,可以看到后面有图片显示,但是被JMenuItem的背景挡住了,而且JMenuItem的背景也没搞好

这是代码
String ico = "UI\\BKIMG.png";

JFrame mini = new JFrame();
MyPanel rootp = new MyPanel();
JLabel beaut = new JLabel(new ImageIcon("UI\\25368_1.gif"));
Point p1_p = new Point(0,0); //用于窗口拖拽
MyPopmenu popm = new MyPopmenu(ico);
MyMenuItem mainp = new MyMenuItem();
JMenuItem restart = new JMenuItem("重启");
JMenuItem dstroy = new JMenuItem("注销");
JMenuItem colse = new JMenuItem("关机");
JMenuItem exit = new JMenuItem("退出");
public void wininit(){

mainp.setStr("张雄");

mainp.setBackImg2("UI\\miniTextA.png");

popm.setPopupSize(55, 225);

popm.setBorderPainted(false);

popm.add(mainp);
popm.addSeparator();
popm.add(restart);popm.addSeparator();
popm.add(dstroy);popm.addSeparator();
popm.add(colse);popm.addSeparator();
popm.add(exit);

rootp.setLayout(null);
rootp.addMouseListener(this);
rootp.addMouseMotionListener(this);
rootp.setSize(40, 40);

beaut.setSize(40, 40);
rootp.add(beaut);
rootp.add(popm);

mini.setContentPane(rootp);
mini.setResizable(false);
mini.setUndecorated(true);
AWTUtilities.setWindowOpaque(mini, false);
mini.setSize(400, 300);
mini.setVisible(true);
}


下面这个是重绘popupmenu的代码
public class MyPopmenu extends JPopupMenu{
   private Image backImg;
    
    
    public MyPopmenu(String imgPath){
     try
     {
     File f = new File(imgPath);
     backImg = ImageIO.read(f);
     }
     catch (IOException e)
     {
     e.printStackTrace();
     }
    }
    
    public MyPopmenu(){
       
    }
    public void SetDefulatSize(){
    
    }
    @Override
     public void paintComponent(Graphics g) {
         super.paintComponent(g);
         if(backImg!=null)
             g.drawImage(backImg, 0, 0,100,50, null);
     }

    public Image getBackImg() {
        return backImg;
    }

    public void setBackImg(Image backImg) {
        this.backImg = backImg;
        this.repaint();
    }
    
    public void setBackImg2(String img2Path) {
     Image backImg2 = null ;
     try
     {
     File f = new File(img2Path);
     backImg2 = ImageIO.read(f);
     }
     catch (IOException e)
     {
     e.printStackTrace();
     }
        this.backImg = backImg2;