简体   繁体   中英

Undecorated JFrame with transparent background?

Is there a way to make an undecorated JFrame with transparent background? I want to make a GUI to control a Lego Mindstorm roboter with the LeapMotion device. Now i need a popup window for some user feedback and option menus.

Here is the code of my popup JFrame class

public Sub_PopUp(int width, int height)
{
    this.width = width;
    this.height = height;

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout());

    this.setBounds(300, 300, this.width, this.height);
    this.setResizable(false);
    this.setUndecorated(true);
    this.setVisible(true);
    this.setOpacity(0.9f);

    contentPane = new JPanel();
    this.setContentPane(contentPane);

    contentPane.setLayout(null);
    contentPane.setBounds(0, 0, this.width, this.height);
    contentPane.setOpaque(false);

    try
    {
        imgLtb = ImageIO.read(new File("img/popup_ltb.png"));
        imgTb = ImageIO.read(new File("img/popup_tb.png"));
        imgRtb = ImageIO.read(new File("img/popup_rtb.png"));
        imgRb = ImageIO.read(new File("img/popup_rb.png"));
        imgRbb = ImageIO.read(new File("img/popup_rbb.png"));
        imgBb = ImageIO.read(new File("img/popup_bb.png"));
        imgLbb = ImageIO.read(new File("img/popup_lbb.png"));
        imgLb = ImageIO.read(new File("img/popup_lb.png"));

    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ltb = new Sub_BackgroundPanel(imgLtb);
    ltb.setOpaque(false);
    ltb.setBounds(0, 0, 35, 35);
    tb = new Sub_BackgroundPanel(imgTb);
    tb.setBounds(35, 0, width-70, 35);
    rtb = new Sub_BackgroundPanel(imgRtb);
    rtb.setBounds(width-35, 0, 35, 35);
    rb = new Sub_BackgroundPanel(imgRb);
    rb.setBounds(width-35, 35, 35, height-70);
    rbb = new Sub_BackgroundPanel(imgRbb);
    rbb.setBounds(width-35, height-35, 35, 35);
    bb = new Sub_BackgroundPanel(imgBb);
    bb.setBounds(35, height-35, width-70, 35);
    lbb = new Sub_BackgroundPanel(imgLbb);
    lbb.setBounds(0, height-35, 35, 35);
    lb = new Sub_BackgroundPanel(imgLb);
    lb.setBounds(0, 35, 35, height-70);
    middle = new JPanel();
    middle.setBounds(35, 35, width-70, height-70);
    middle.setBackground(Color.decode("#9db4bd"));


    contentPane.add(middle);
    contentPane.add(ltb);
    contentPane.add(tb);
    contentPane.add(rtb);
    contentPane.add(rb);
    contentPane.add(rbb);
    contentPane.add(bb);
    contentPane.add(lbb);
    contentPane.add(lb);

}

I think this link will works for you.

undecorated-translucent-and-shaped-window-in-java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM