我需要在 java swing 中创建一个透明的 jframe,它的不透明度为 0.05f。我尝试了下面的代码,但它不起作用。我在窗户工作。
我需要做什么才能使其工作?
import java.awt.AlphaComposite;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import java.awt.Color;
public class BackgroundNull {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BackgroundNull window = new BackgroundNull();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public BackgroundNull() {
initialize();
private void initialize() {
frame = new JFrame();
frame.setBackground(new Color(0, 0, 0, 0));
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setOpacity(0.5f);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f));
frame.getContentPane().paint(g2);
g2.dispose();
}
}