Java: Transparent Windows with non-transparent components?

僤鯓⒐⒋嵵緔 提交于 2019-12-12 12:15:46

问题


I just met the utilities (com.sun.awt.AWTUtilities) to make your JFrame really transparent. Documentation here. This works very good. Even in Linux with the desktop effects with wobbly windows turned on. But I want to make also a non-transparent component on the transparent JFrame.

Does anyone know, if this is possible, how?

Here is the code I used:

import com.sun.awt.AWTUtilities;

/* "this" is the JFrame */
this.setUndecorated(true);
AWTUtilities.setWindowOpaque(this, true);
AWTUtilities.setWindowOpacity(this, 0.5f);
AWTUtilities.setWindowShape(this, new RoundRectangle2D.Float(0f, 0f, (float) getWidth(), (float) getHeight(), 15f, 15f));

回答1:


IIUC, Translucent Windows applies to the entire java.awt.Window and contents, but you might try the approach shown below and in this example.

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBackground(new Color(0f, 0f, 0f, 0.1f));
f.setUndecorated(true);
f.add(new JLabel("<html>Testing<br>1, 2, 3</html>"));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);



回答2:


What you want to do is setWindowOpaque to false and draw the background with low alpha (or 0 alpha if you want complete transparency). The components will still be drawn opaque. See this article and look at per-pixel translucency.



来源:https://stackoverflow.com/questions/3372016/java-transparent-windows-with-non-transparent-components

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