我正在用java创建一个圆角的登录窗口。一切都很好,见图片,但我在使 JFrame / ContentPane 透明时遇到了挑战。每个角落都有一些白色区域(如箭头所示),我似乎无法移除这些区域,因为我无法将 JFrame 或 ContentPane 的 opague 设置为 false。
问问题
9071 次
4 回答
2
Since Java 1.3 there's a trick which allows to make partially transparent windows, or windows fading in (I usually use this for my splash screens), or special FX (such as shadows):
- Before opening the window, programmatically take a screenshot of the region where your window is going to be (using java.awt.Robot.createScreenCapture())
- Set the screenshot as the background of your root container (JPanel with custom paintComponent() routine)
- Now you can add all kinds of transparent components, or paint another semitransparent image on top of the background.
Example which creates a window with a semitransparent shadow using this technique: http://www.eclipsezone.com/eclipse/forums/t17720.html
于 2009-06-05T08:16:42.133 回答
2
对您没有多大帮助,但 Java7 将支持透明和形状窗口:更多信息在这里。这些已经在 Java 6u10 中可用,但不是公开的,即,您需要使用不受支持的 com.sun... 类,该类将来可能会更改并破坏您的程序。
于 2009-06-05T08:32:08.420 回答
1
尝试这个。这是工作 :)
yourframe.setBackground(new Color(0, 0, 0, 180));
yourframe.setUndecorated(true);
yourframe.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 80, 80));
}
});
于 2015-03-08T09:18:56.173 回答
0
JFrame 不能透明,因为它是重量级组件。只有像 JWindow 这样的轻量级组件才能透明。
于 2009-06-05T07:49:06.743 回答