[英]How to create background of JFrame transparent in java but keeping buttons visible?
我通过以下方式使我的 JFrame 透明:
myFrame.setUndecorated(true);
myFrame.setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
它使 JFrame 透明,但也丢失了窗口条和关闭按钮。 为了使窗口可见,我用过
AWTUtilities.setOpacity(this, 0.5f);
但是我发现这个方法只适用于java 6并且打包在java 7中限制的AWT包中。并且该方法在java 7中也发生了变化,现在是
Window.setOpacity(float opacity);
但它不起作用,谁能告诉我如何使透明窗口和按钮与透明框架一起可见。
这将使框架看起来有点不同,但我认为这是你想要的:
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TransparentExample extends JFrame {
public TransparentExample() {
super("TransparentExample");
JPanel panel = new JPanel();
panel.add(new JButton("Button"));
setContentPane(panel);
setBackground(new Color(0, 0, 0, 0));
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
TransparentExample frame = new TransparentExample();
frame.setVisible(true);
}
}
如果它有效,它应该是这样的:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.