简体   繁体   English

设置JFrame背景透明使PopupMenu空白

[英]Setting the JFrame background transparent making PopupMenu blank

My question is similar to this , but I think has a simpler example. 我的问题与类似,但我认为有一个更简单的例子。

Basically by calling AWTUtilities.setWindowOpaque(window, false) to make the background of the JFrame transparent, my JPopupMenu sometimes show up as blank. 基本上通过调用AWTUtilities.setWindowOpaque(window, false)使JFrame的背景透明,我的JPopupMenu有时会显示为空白。

public class JavaApplication8 {

    JPopupMenu popup;
    JMenuItem open;
    JLabel bgLabel = new JLabel("testing");

    public static void main(String[] args) {
        // TODO code application logic here

        JFrame window = new JFrame("test");

        URL bgURL = JavaApplication8.class.getResource("images/bg.jpg");
        ImageIcon bg = new ImageIcon(bgURL);

        JavaApplication8 test = new JavaApplication8();
        test.setPopupMenu();
        test.bgLabel.setIcon(bg);

        window.add(test.bgLabel, BorderLayout.CENTER);

        window.setUndecorated(true);
        AWTUtilities.setWindowOpaque(window, false);        
        //window.pack();
        window.setSize(200, 200);
        window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        window.setLocationRelativeTo(null);
        window.setVisible(true);

    }

    public void setPopupMenu(){
        popup = new JPopupMenu();
        open = new JMenuItem("Test");

        popup.add(open);
        this.bgLabel.setComponentPopupMenu(popup);     
    }

}

Here's an image of what's happening: 这是一个正在发生的事情的图像:

在此输入图像描述在此输入图像描述

What's interesting is that this happens whenever I click on the right side of the JFrame. 有趣的是,每当我点击JFrame的右侧时就会发生这种情况。 Not sure why. 不知道为什么。 Keep in mind I'm not 100% sure that AWTUtilities.setWindowOpaque(window, false) is indeed the cause of this problem, however whenever I delete that line everything seems to be going fine. 请记住,我不是100%确定AWTUtilities.setWindowOpaque(window, false)确实是这个问题的原因,但每当我删除该行时,一切似乎都很顺利。

EDIT: As stated by camickr , looks like this happens when the popup menu is not fully contained in the bounds of the parent window. 编辑:正如camickrlooks like this happens when the popup menu is not fully contained in the bounds of the parent window.

this happens whenever I click on the right side of the JFrame 只要我点击JFrame的右侧,就会发生这种情况

Looks like this happens when the popup menu is not fully contained in the bounds of the parent window. 当弹出菜单未完全包含在父窗口的边界中时,会发生这种情况。 No idea how to fix this. 不知道如何解决这个问题。

In Java 7 you can use: 在Java 7中,您可以使用:

frame.setBackground(new Color(0, 0, 0, 0));

for transparency. 透明度。

Background: I'm not sure why using transparent / semi-transparent backgrounds causes problems with heavyweight popups and how they paint, but it does -- regarless of whether or not you use AWTUtilities.setWindowOpaque(window, false) or frame.setBackground(new Color(0, 0, 0, 0)) . 背景:我不确定为什么使用透明/半透明背景会导致重量级弹出窗口以及它们如何绘制问题,但确实如此 - 无论您是否使用AWTUtilities.setWindowOpaque(window, false)frame.setBackground(new Color(0, 0, 0, 0))

The HeavyWeightPopup s get created when a popup can't fit all the way inside the target Window. 当弹出窗口无法完全适合目标窗口时,会创建HeavyWeightPopup So +User2280704 your problem also presents if you click at the very bottom of your window. 所以+ User2280704如果你点击窗口的最底部,你的问题也会出现。 LightWeightPopup s do not have this problem -- hence, menus work in the middle of your window. LightWeightPopup没有这个问题 - 因此,菜单在窗口中间工作。

Also, interesting to note, typically the menu will render fine the first time, just not the following times. 另外,有趣的是,通常菜单会在第一次渲染时很好,而不是以下时间。

Answer: I've come up with a workaround that invokes a repaint after any popups display. 答:我已经提出了一个解决方法,在弹出任何弹出窗口后调用重绘。 Simply invoke the following code when you launch your application. 只需在启动应用程序时调用以下代码即可。

PopupFactory.setSharedInstance(new PopupFactory() 
{
    @Override
    public Popup getPopup(Component owner, final Component contents, int x, int y) throws IllegalArgumentException
    {
        Popup popup = super.getPopup(owner, contents, x, y);
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                contents.repaint();
            }
        });
        return popup;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM