如何使JFrame透明?我想使我的JFrame透明。当我的JFrame位于顶部时,用户应该会看到背景。
我找到了另一个解决方案。
将框架的背景色设置为
// Set the frame background color to a transparent color
yourFrameHere.setBackground(new Color(0, 0, 0, 0));
并且记住要关闭contentpane(您的JPanel或其他组件)的不透明度
// turn off opacity of the content pane
yourContentPaneHere.setOpaque(false);
如果您对使用受限API类没有异议,则可以使用该类的AWTUtilities
类和该方法的setWindowOpacity()
方法来做到这一点。 Here和here是有关如何使用它的教程? here是使用Java本机访问的版本。
代码示例
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);
}
});
}