AWTUtilities Transparent JFrame

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

I have one image inside the label on the frame. I want the image to be visible, but the frame is 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();
}

This makes the transparency transparent so that the components are not transparent.

+3
source share
4 answers

You can try simply setting the alpha channel for the background color of your frame, which should not fall on the components.

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

should give you a white transparent window.

+1
source

AWTUtilities, , setWindowOpacity() setWindowOpaque(). , , , - -. Java 6, -. AWTUtilities Mac, MS Windows. java.awt.Window Java 7.

+1

, -

childComponent.setOpaque(true);

, .

0

I would like to expand on the previous answer with the following. This will create a window with a transparency of 0.05 or 12.75 out of 255. Then the components will be set to a transparency of 0.50f, this will only affect clicked components. Without clicks like shortcuts, their transparency can be set. However, this fixes problems with clickable components that it changes 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);
0
source

Source: https://habr.com/ru/post/1713867/


All Articles