简体   繁体   中英

How to make JFrame same shape as Image with transparent Background?

I am a Beginner in Java using NetBeans as my IDE...

I am trying to make a custom shaped JFrame that is shaped like the Image I will be creating... I found a solution here and here but I can't figure out how to apply it in Netbeans..

Took me hours and hours of research but to no avail.. So I asked it here hoping someone would enlighten me... I also hope you explain the codes used and how it worked so I would also learn and just copy pasting...

I also hope you explain the codes used and how it worked

That is the benefit of a tutorial. Read the section from the Swing tutorial on How to Create Translucent and Shaped Windows for information and working examples.

I can't figure out how to apply it in Netbeans

Don't use the IDE to generate your code. If you ever switch IDE's then you need to learn a new one. Instead spend the time learning Java/Swing, not the IDE.

After a couple of hours Playing around with NetBeans after reading and watching couple of tutorials I have found, I finally got a simple solution...

Here is the code:

package testPack;

import java.awt.Color;
import javax.swing.*;
import java.awt.Dimension;

/**
 *
 * @author Karyuu Ouji
 */
public class TestPanel extends javax.swing.JPanel {

    /**
     * Creates new form TestPanel
     */
    public TestPanel() {

        initComponents();
        this.setPreferredSize(new Dimension(704,182));
        this.setOpaque(false);
        this.setDoubleBuffered(true);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setLayout(null);

        jButton1.setText("Close");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton1MouseReleased(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(593, 0, 110, 23);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/testPack/sao.png"))); // NOI18N
        jLabel1.setText("jLabel1");
        add(jLabel1);
        jLabel1.setBounds(0, 0, 704, 180);
    }// </editor-fold>                        

    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {                                       
        System.exit(0);
    }                                      

    public static void main(String[] args) {

        JFrame frmMain = new JFrame();
        frmMain.setUndecorated(true);
        frmMain.setBackground(new Color(0,0,0,0));
        frmMain.add(new TestPanel());
        frmMain.pack();
        frmMain.setLocationRelativeTo(null);
        frmMain.setVisible(true);

    }


    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
}

Note: Most of the code are generated by NetBeans

All I did was add a JLabel from the component pallet and set the size of the JPanel and JLabel the same as the image size.

Then I put on the image in the JLabel via the Icon property.

Setting Icon/Image

And here is what it looks like when I run the app.

Running

I hope this will help someone like me in the future... :)

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