Create a transparent JFrame in java swing

Hi everyone, I made a swing application in java, but the problem is that I can achieve a transparent frame in java.

I want to create a user interface design, like in an attached image, but the problem is that the frames are not transparent, so it shows the default color if there are no components at this level.

As on the right side, I want to show this frame transparent in the upper free and lower free parts,

I use AWTUtilities, but this does not work for me.

how can i do this, plz answer,

also give me a tip to drag the window along the title bar, in an undeclared window

early enter image description here

+4
source share
1 answer

Transparent Background

public class TransparentBackground extends Jcomponent { private JFrame frame; private Image background; public TransparentBackground(JFrame frame) { this.frame = frame; updateBackground( ); } public void updateBackground( ) { try { Robot rbt = new Robot( ); Toolkit tk = Toolkit.getDefaultToolkit( ); Dimension dim = tk.getScreenSize( ); background = rbt.createScreenCapture( new Rectangle(0,0,(int)dim.getWidth( ), (int)dim.getHeight( ))); } catch (Exception ex) { p(ex.toString( )); ex.printStackTrace( ); } } public void paintComponent(Graphics g) { Point pos = this.getLocationOnScreen( ); Point offset = new Point(-pos.x,-pos.y); g.drawImage(background,offset.x,offset.y,null); } } 

You can start this with a simple main () method, dropping a few components on the panel and placing them in a frame:

 public static void main(String[] args) { JFrame frame = new JFrame("Transparent Window"); TransparentBackground bg = new TransparentBackground(frame); bg.setLayout(new BorderLayout( )); JButton button = new JButton("This is a button"); bg.add("North",button); JLabel label = new JLabel("This is a label"); bg.add("South",label); frame.getContentPane( ).add("Center",bg); frame.pack( ); frame.setSize(150,100); frame.show( ); } 
+3
source

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


All Articles