我的问题与此类似,但我认为有一个更简单的例子。
基本上通过调用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);
}
}
这是正在发生的事情的图像:
有趣的是,每当我单击 JFrame 的右侧时,都会发生这种情况。不知道为什么。请记住,我不是 100% 确定这AWTUtilities.setWindowOpaque(window, false)
确实是导致此问题的原因,但是每当我删除该行时,一切似乎都很好。
编辑:如前所述camickr
,looks like this happens when the popup menu is not fully contained in the bounds of the parent window.