Java 刷新完全透明的JFrame

Java 刷新完全透明的JFrame,java,swing,graphics,transparent,graphics2d,Java,Swing,Graphics,Transparent,Graphics2d,我想做一个类似smth的窗口——我有完全透明的JFrame和JPanel,用鼠标移动窗口,但不会刷新面板——很快窗口就会变成完全红色。这是我的密码: public class Main { public static HolePanel panel = new HolePanel(); public static JFrame frame = new JFrame(); public static void main(String[] args) {

我想做一个类似smth的窗口——我有完全透明的JFrame和JPanel,用鼠标移动窗口,但不会刷新面板——很快窗口就会变成完全红色。这是我的密码:

public class Main {

    public static HolePanel panel = new HolePanel();
    public static JFrame frame = new JFrame();

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setUndecorated(true);
        frame.setBackground(new Color(0, 0, 0, 0));
        frame.setAlwaysOnTop(true);
        frame.setBounds(0, 0, (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
        frame.setContentPane(panel);

        frame.addMouseMotionListener(new MouseList());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}



public class HolePanel extends JPanel {

public double centerX = 200;
public double centerY = 200;
public double diameter = 300;

HolePanel(){
    setBackground(new Color(0, 0, 0, 0));
    repaint();
}

public void paintComponent(Graphics badG){
    super.paintComponent(badG);
    Graphics2D g = (Graphics2D)badG;

    g.setColor(Color.RED.darker().darker());

    Area a = new Area (new Rectangle2D.Double(0, 0, (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()));
    a.subtract(new Area(new Ellipse2D.Double(centerX - diameter / 2, centerY - diameter / 2, diameter,diameter)));
    g.fill(a);
}

}


public class MouseList implements MouseMotionListener {

public void mouseDragged(MouseEvent e) {
}

@Override
public void mouseMoved(MouseEvent e) {
    Main.panel.centerX = e.getXOnScreen();
    Main.panel.centerY = e.getYOnScreen();  
    Main.panel.repaint();
}

}
你们能帮我个忙吗?

退房。也许您可以使用
AlphaContainer

基本上,如果要使用透明背景,则必须使组件不透明,并重写paintComponent()方法来绘制背景。
AlphaContainer
为您执行此操作。

setOpaque(false)有帮助