如何在Java中创建覆盖窗口? [英] How to create an overlay window in Java?

查看:181
本文介绍了如何在Java中创建覆盖窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为外部应用程式建立HUD样式显示。

I'm trying to create a HUD style display for a foreign application.

为了做到这一点,我需要创建一个透明的重叠窗口,放置在外部应用程序窗口的顶部
上。覆盖窗口应该允许我放置
小部件并在其上绘制文本。事件应该被转发到底层窗口
如果它们发生在透明区域(否则允许小部件工作作为
预期)。

To do this, I'd need to make a transparent overlay window, that would be placed on top of the window of the foreign application. The overlay window should allow me to place widgets and draw text on it. Events should get forwarded to the underlying window, if they happen on the transparent area (and otherwise allow the widgets to work as expected).

我在OSX上用Java做这个。我希望这样做与纯Java和可移植到
其他平台,但如果不可能,我会很好的解决方案,只是允许
我在OSX通过Cocoa(Rococoa )或碳。

I'm doing this on OSX with Java. I'd hope to do this with pure Java with portability to other platforms, but if not possible, I'd be fine with solutions that would just allow me to do this on OSX through Cocoa (Rococoa) or Carbon.

推荐答案

其实,看起来比我预想的容易:

Actually, was able to figure this out myself. Seems to be easier than I expected:

public class Overlay {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Transparent Window");
        frame.setUndecorated(true);
        frame.setBackground(new Color(0, 0, 0, 0));
        frame.setAlwaysOnTop(true);
        // Without this, the window is draggable from any non transparent
        // point, including points  inside textboxes.
        frame.getRootPane().putClientProperty("apple.awt.draggableWindowBackground", false);

        frame.getContentPane().setLayout(new java.awt.BorderLayout());
        frame.getContentPane().add(new JTextField("text field north"), java.awt.BorderLayout.NORTH);
        frame.getContentPane().add(new JTextField("text field south"), java.awt.BorderLayout.SOUTH);
        frame.setVisible(true);
        frame.pack();
    }
}

这篇关于如何在Java中创建覆盖窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆