`
稻-草
  • 浏览: 61952 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

自定义Popup window.

 
阅读更多

基类:采用JFrame + setUndecorated(true);

 

import java.awt.Graphics;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class PopupWindow extends JFrame
{
    public PopupWindow()
    {
        setUndecorated(true);

        // setAlwaysOnTop(true);
        addWindowFocusListener(new WindowFocusListener()
        {
            public void windowGainedFocus(WindowEvent e)
            {

            }

            public void windowLostFocus(WindowEvent e)
            {
                SwingUtilities.invokeLater(new Runnable()
                {
                    public void run()
                    {
                        setVisible(false);
                        dispose();
                    }
                });
            }
        });

    }

    public void showPopup(int x, int y)
    {
        setLocation(x, y);
        setVisible(true);
        requestFocus();
    }

    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
    }
}

 通过继承PopupWindow,就可以试下模拟弹出菜单。

唯一的缺点是没有阴影,看起来没有立体感。

 

效果图:


 

 这里有模拟阴影的方法, 还没试过:

http://stackoverflow.com/questions/19105242/undecorated-jframe-shadow

 

  • 大小: 11.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics