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

可紊 提交于 2019-12-12 16:34:26

问题


I created transparent JDialog which unfortunately does not work with two screens. 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


回答1:


I created transparent 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. Check out Backgrounds With Transparency for more information.

Instead of playing with transparent Colors, try using:

dialog.setOpacity(...);



回答2:


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. Although it's a single monitor setup (the MacBook screen), switching between desktops would "burn" the shadows into the window display.

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



来源:https://stackoverflow.com/questions/36130906/transparent-jdialog-becomes-opaque-when-dragging-to-second-screen-ubuntu-14-04

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!