简体   繁体   English

拖动到第二个屏幕时,透明的JDialog变得不透明(带有Cinnamon,Java 1.8.0_74-b02的Ubuntu 14.04)

[英]transparent JDialog becomes opaque when dragging to second screen (ubuntu 14.04 with Cinnamon, java 1.8.0_74-b02)

I created transparent JDialog which unfortunately does not work with two screens. 我创建了透明的JDialog,不幸的是,该对话框无法在两个屏幕上运行。 When its dragged to other screen it becomes opaque. 当将其拖动到其他屏幕时,它变得不透明。 The code is below, just run it and drag label to other screen. 代码在下面,只需运行它并将标签拖到其他屏幕即可。

public class TransparentFrame{

public static void main(String[] args) {
    JDialog dialog = createDialog();
    SwingUtilities.invokeLater(() -> dialog.setVisible(true));
}


private static JDialog createDialog() {
    JDialog dialog = new JDialog();

    JLabel label = new JLabel("drag me to the other screen");
    label.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            SwingUtilities.invokeLater(() -> dialog.setLocation(e.getLocationOnScreen()));
        }
    });
    label.setOpaque(false);
    dialog.getContentPane().add(label);

    dialog.setUndecorated(true);
    dialog.getRootPane().setWindowDecorationStyle(JRootPane.NONE);

    dialog.setBackground(new Color(0, 0, 0, 0));
    dialog.getContentPane().setBackground(new Color(0, 0, 0, 0));

    dialog.pack();
    return dialog;
}

} }

Does anybody know how to fix it? 有人知道如何解决吗?

Environment: Ubuntu 14.04 with Cinnamon, java 1.8.0_74-b02 环境:Ubuntu 14.04 with Cinnamon,Java 1.8.0_74-b02

I created transparent JDialog 我创建了透明的JDialog

Don't know if it is a problem in this case but Swing and transparent Colors don't get along because you are breaking the painting contract between Swing and its components. 不知道在这种情况下是否有问题,但是Swing和透明的Colors无法相处,因为您违反了Swing及其组件之间的绘画协定。 Check out Backgrounds With Transparency for more information. 请查看具有透明度的背景,以获取更多信息。

Instead of playing with transparent Colors, try using: 而不是使用透明的颜色,请尝试使用:

dialog.setOpacity(...);

Although this may or may not be the source of the problem, it's best to remove the shadow of a transparent window. 尽管这可能是问题的根源,也可能不是问题的根源,但最好是清除透明窗口的阴影。 Without these lines, my program (on Mac) would "burn" the shadows into the window display when interrupted. 如果没有这些行,我的程序(在Mac上)将在中断时将阴影“燃烧”到窗口显示中。 Although it's a single monitor setup (the MacBook screen), switching between desktops would "burn" the shadows into the window display. 尽管它是单个显示器设置(MacBook屏幕),但在台式机之间切换会把阴影“烧入”到窗口显示中。

JRootPane root = frame.getRootPane(); root.putClientProperty("Window.shadow", Boolean.FALSE);

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

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