具有黑色不透明度的 JWindow

发布于 2024-09-06 16:36:49 字数 264 浏览 6 评论 0原文

我想创建一个 JWindow,它不仅具有不透明度,而且还想更改 Swing 内不透明度的默认颜色。

例如,如果我写:

AWTUtilities.setWindowOpacity(this, 0.5f);

这将完全按照我想要的方式进行,但有一个例外,颜色是白色。怎样才能让颜色变成黑色呢?

我已经在“this”上尝试了 setBackground(Color.Black) 等所有内容...

I would like to create a JWindow that's not only has an opacity, but I want to change the default color of the opacity within Swing.

So for example, if I write:

AWTUtilities.setWindowOpacity(this, 0.5f);

This will make do exactly what I want with one exception, the color is white. How can I make the color become black?

I've tried everything from setBackground(Color.Black), etc. on "this"...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开雨落又逢春i 2024-09-13 16:36:49
        window.getContentPane().setBackground(Color.BLACK);
        window.getContentPane().setBackground(Color.BLACK);
梦在夏天 2024-09-13 16:36:49

透明 JWindow。

public class TransparentWindow{

    JWindow window;

    public TransparentWindow(){
        initializeTransparentWindow();
    }

    private void initializeTransparentWindow() {
        // searching graphical configuration that provide transparent window
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] devices = env.getScreenDevices();
        GraphicsConfiguration translucencyCapableGC = null;
        for (int i = 0; i < devices.length && translucencyCapableGC == null; i++) {
            GraphicsConfiguration[] configs = devices[i].getConfigurations();
            for (int j = 0; j < configs.length && translucencyCapableGC == null; j++) {
                if (AWTUtilities.isTranslucencyCapable(configs[j])) {
                    translucencyCapableGC = configs[j];
                }
            }
        }
        if (translucencyCapableGC != null) {
            window = new JWindow(translucencyCapableGC) {
                @Override
                public void paint(Graphics g) {
                    if (getWidth() > 4 && getHeight() > 4) {
                        g.clearRect(0, 0, getWidth(), getHeight());
                        g.setColor(new Color(0x0, 0x0, 0x0, 0xaa));
                        g.fillRect(0, 0, 1, getHeight());
                        g.fillRect(0, 0, getWidth(), 1);
                        g.fillRect(0, getHeight() - 1, getWidth(), 1);
                        g.fillRect(getWidth() - 1, 0, 1, getHeight());
                        g.setColor(new Color(0x0, 0x0, 0x0, 0x10));
                        g.fillRect(1, 1, getWidth() - 1, getHeight() - 1);
                    }
                };
            };
            AWTUtilities.setWindowOpaque(window, false);
        }
        else {
            window = new JWindow();
            AWTUtilities.setWindowOpacity(window, 0.5f);
        }
    }

Transparent JWindow.

public class TransparentWindow{

    JWindow window;

    public TransparentWindow(){
        initializeTransparentWindow();
    }

    private void initializeTransparentWindow() {
        // searching graphical configuration that provide transparent window
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] devices = env.getScreenDevices();
        GraphicsConfiguration translucencyCapableGC = null;
        for (int i = 0; i < devices.length && translucencyCapableGC == null; i++) {
            GraphicsConfiguration[] configs = devices[i].getConfigurations();
            for (int j = 0; j < configs.length && translucencyCapableGC == null; j++) {
                if (AWTUtilities.isTranslucencyCapable(configs[j])) {
                    translucencyCapableGC = configs[j];
                }
            }
        }
        if (translucencyCapableGC != null) {
            window = new JWindow(translucencyCapableGC) {
                @Override
                public void paint(Graphics g) {
                    if (getWidth() > 4 && getHeight() > 4) {
                        g.clearRect(0, 0, getWidth(), getHeight());
                        g.setColor(new Color(0x0, 0x0, 0x0, 0xaa));
                        g.fillRect(0, 0, 1, getHeight());
                        g.fillRect(0, 0, getWidth(), 1);
                        g.fillRect(0, getHeight() - 1, getWidth(), 1);
                        g.fillRect(getWidth() - 1, 0, 1, getHeight());
                        g.setColor(new Color(0x0, 0x0, 0x0, 0x10));
                        g.fillRect(1, 1, getWidth() - 1, getHeight() - 1);
                    }
                };
            };
            AWTUtilities.setWindowOpaque(window, false);
        }
        else {
            window = new JWindow();
            AWTUtilities.setWindowOpacity(window, 0.5f);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文