在绘制时,摆动屏幕叠加干扰鼠标

问题描述 投票:0回答:1

我有一个JFrame,透明总是在顶部,不可聚焦,所以我可以用它作为屏幕覆盖。它的工作原理,但问题是:当我把光标放在某个地方时,我画了一些东西,我的光标总是“在'上面',所以我无法点击实际上对焦的窗口。有没有什么办法解决这一问题?

public class ExternalOverlay extends JFrame implements ActionListener {

    public Timer timer;
    private float[] res;
    private final int FRAMERATE = 60;

    public ExternalOverlay() {
        this.setUndecorated(true);
        this.setBackground(new Color(0, 0, 0, 0));
        float[] res = this.getRes();
        this.setBounds(0, 0, (int)res[0], (int)res[1]);
        this.setAlwaysOnTop(true);
        this.getContentPane().setLayout(new java.awt.FlowLayout());
        this.setVisible(true);
        this.setAutoRequestFocus(false);
        this.setFocusableWindowState(false);
        this.timer = new Timer(1000/FRAMERATE, this);
        timer.start();
    }

    private float[] getRes() {
        if (this.res != null) return res;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double width = screenSize.getWidth();
        double height = screenSize.getHeight();
        return new float[] {(float) width, (float) height};
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        this.repaint();
    }


    public void draw(Graphics g) {

    }

    @Override
    public void paint(Graphics g) {
    // draw
    }
}
java swing
1个回答
0
投票

我有一个透明的JFrame

你应该使用:

frame.setOpacity(0.0f);

阅读How to Create Translucent and Shaped Windows上的Swing教程中的部分。下载TranslucentWindowDemo代码,以便开始使用。

© www.soinside.com 2019 - 2024. All rights reserved.