问题
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