AWTUtilities 透明 JFrame

发布于 2024-07-29 08:26:45 字数 906 浏览 8 评论 0原文

使用 Sun 的这篇文章。 我正在尝试创建一个透明的窗口。

我在框架上的标签内有一张图像。 我希望图像可见,但框架不可见。

当我使用


try {
   Class awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
   Method mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);
   mSetWindowOpacity.invoke(null, window, Float.valueOf(0.75f));
} catch (NoSuchMethodException ex) {
   ex.printStackTrace();
} catch (SecurityException ex) {
   ex.printStackTrace();
} catch (ClassNotFoundException ex) {
   ex.printStackTrace();
} catch (IllegalAccessException ex) {
   ex.printStackTrace();
} catch (IllegalArgumentException ex) {
   ex.printStackTrace();
} catch (InvocationTargetException ex) {
   ex.printStackTrace();
}

它使一切透明时,可以保持组件不透明。

Using this article from sun. I am trying to create a transparent window.

I have one image inside a label on the frame.
I want the image to be visible but the frame invisible.

When i use


try {
   Class awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
   Method mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);
   mSetWindowOpacity.invoke(null, window, Float.valueOf(0.75f));
} catch (NoSuchMethodException ex) {
   ex.printStackTrace();
} catch (SecurityException ex) {
   ex.printStackTrace();
} catch (ClassNotFoundException ex) {
   ex.printStackTrace();
} catch (IllegalAccessException ex) {
   ex.printStackTrace();
} catch (IllegalArgumentException ex) {
   ex.printStackTrace();
} catch (InvocationTargetException ex) {
   ex.printStackTrace();
}

It makes everthing transparent is possible to keep components not transparent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅听莫相离 2024-08-05 08:26:45

您可以尝试仅设置框架背景颜色的 Alpha 通道,这不应下降到组件。

window.setBackground(new Color(1.0, 1.0, 1.0, 0.25));

应该给你一个白色透明的窗口。

You could try just setting the alpha channel for the background color of your frame, that shouldn't descend to components.

window.setBackground(new Color(1.0, 1.0, 1.0, 0.25));

should give you a white, transparent window.

月隐月明月朦胧 2024-08-05 08:26:45

您仍然可以使用 AWTUtilities 类,但不要使用 setWindowOpacity() setWindowOpaque() 设置不透明度。 这将关闭窗口的背景,但您在窗口内绘制的任何内容仍将像以前一样绘制。 从最近的 Java 6 更新来看,这是现在正确的处理方式。 AWTUtilities 可在 Mac 和 Mac 上运行。 微软视窗。 这些方法将被移至 Java 7 中的 java.awt.Window 本身。

You can still use the AWTUtilities class, but instead of setting the opacity with setWindowOpacity() setWindowOpaque(). This will turn off the background of the window, but anything you draw inside the window will still be drawn as before. As of the recent Java 6 updates this is now the correct way to do things. AWTUtilities will work on both Mac & MS Windows. These methods will be moved into java.awt.Window itself in Java 7.

撩心不撩汉 2024-08-05 08:26:45

您需要设置子组件的不透明度,例如

childComponent.setOpaque(true);

That 将使它们不透明,而不使框架不透明。

You need to set the opacity of the child components, something like

childComponent.setOpaque(true);

That will make them opaque, without making the frame opaque.

倒带 2024-08-05 08:26:45

我想通过以下内容扩展之前的答案。 这将创建一个透明度为 0.05 或 255 中的 12.75 的窗口。然后将组件的透明度设置为 0.50f,这只会影响可单击的组件。 不可点击的标签可以将其透明度设置为任意值。 然而,这解决了可点击组件改变颜色的问题。

JWindow subFrame = new JWindow();           
subFrame.setBounds(0, 0, 500, 500);
subFrame.setAlwaysOnTop(true);
subFrame.setOpacity(0.50f);
subFrame.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.05f));

JButton button = new JButton("Hello");
button.setBounds(20, 180, 100, 40);

subFrame.getContentPane().setLayout(null);
subFrame.getContentPane().add(button);
subFrame.setVisible(true);

I'd like to expand on a previous answer with the following. This will create a window with 0.05 transparency or 12.75 out of 255. Then the components are set to a transparency of 0.50f this will only effect clickable components. Non clickable like Labels can have their transparency set to what ever. This however patches the clickable components problem of it changing colors.

JWindow subFrame = new JWindow();           
subFrame.setBounds(0, 0, 500, 500);
subFrame.setAlwaysOnTop(true);
subFrame.setOpacity(0.50f);
subFrame.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.05f));

JButton button = new JButton("Hello");
button.setBounds(20, 180, 100, 40);

subFrame.getContentPane().setLayout(null);
subFrame.getContentPane().add(button);
subFrame.setVisible(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文