How to make JFrame transparent?

后端 未结 2 1979
失恋的感觉
失恋的感觉 2021-01-04 20:36

How to make JFrame transparent? I want to make my JFrame transparent. User should see the background when my JFrame is on top of it.

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 21:02

    If you do not have any objection in using restricted API classes then you can do this with AWTUtilities class and setWindowOpacity() method of that class. Here and here is a tutorial on how to use it? And here is the version using Java native access.

    code example

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                javax.swing.JFrame fr = new NewJFrame();
                com.sun.awt.AWTUtilities.setWindowOpacity(fr, 0.7 f);
                fr.setVisible(true);
            }
        });
    }
    

提交回复
热议问题