Java - ol'透明的 JFrame,无限制的库

发布于 2024-12-07 17:20:54 字数 1376 浏览 1 评论 0原文

是的,这个问题无处不在。但所有(工作)解决方案都使用 AWTUtilities 工具包,该工具包受到限制。

所以。我只想控制窗口的不透明度。无需塑形或取消装饰。只是一个透明的 contentPane(简单)和一个透明的 JFrame 背景(极其困难)。

我昨天可以发誓我得到了正确的东西组合,但现在我似乎无法重现它。还有一些不使用 AWTUtilities 的解决方案,但它们不起作用......有人有好的解决方案吗?

我的失败代码的一个例子:

public static void main(String[] args) {
    JFrame test = new JFrame();
    test.setSize(400, 400);
    test.setLocationRelativeTo(null);
    test.setUndecorated(true);
    test.setBackground(new Color(0, 0, 0, 0));
    test.getContentPane().setBackground(new Color(0,0,0,0));
    test.setVisible(true);
}

但这只是一个白色的方块。关闭但没有雪茄。我也尝试过重写绘画方法,有人说要扔掉阿尔法通道,但这使它变成黑色(当然)。所以...堆栈溢出就是这样。

如果有一个骗子准确回答了这个问题,请指出它,我会立即删除。

更新 根据评论中的请求,以下是我如何到达这里的历史:

每个人到达的第一个链接是 如何创建半透明和成形的窗口。其中有几行类似于“...框架半透明,不透明度为 75%”。所以...看起来它们的定义是相同的,至少在那篇文章中是这样。不幸的是他们使用图书馆。

我可以追踪几个链接:

http://www.java-gaming.org/index.php?topic=24635.0 这显示了一些希望,但我无法让它工作。

http://techgearup.wordpress.com/2008/09/19/透明-jframe-background/ 是一种使用屏幕截图的黑客方式,并且在其他一些地方重复。

Yes, this question is everywhere. But all of the (working) solutions use the AWTUtilities toolkit, which is restricted.

So. I just want to control the opacity of my window. No need to shape or undecorate. Just a transparent contentPane (easy), and a transparent JFrame background (ridiculously difficult).

I could have sworn I got the right combination of stuff yesterday, but now I can't seem to reproduce it. Also there are some solutions out there without using AWTUtilities, but they don't work...does someone have a good solution?

An example of my failing code:

public static void main(String[] args) {
    JFrame test = new JFrame();
    test.setSize(400, 400);
    test.setLocationRelativeTo(null);
    test.setUndecorated(true);
    test.setBackground(new Color(0, 0, 0, 0));
    test.getContentPane().setBackground(new Color(0,0,0,0));
    test.setVisible(true);
}

but this just makes a white square. Close but no cigar. I've also tried overriding the paint method, and someone was saying something about throwing out the alpha channel, but that made it black (of course). So...Stack Overflow it is.

If there's a dupe that answers this exactly, please point me to it and I'll delete right away.

Update
Per requests in comments, here's a history of how I arrived here:

The first link everyone arrives at is how to create translucent and shaped windows. This has several lines in there similar to "...the frame translucent with a 75% level of opacity." So...looks like they're defined the same, at least in that article. Unfortunately they use the library.

A couple links that I could chase down:

A non-working "working" solution is reported at http://www.java-gaming.org/index.php?topic=24635.0 which shows some hope but I couldn't get it working.

http://techgearup.wordpress.com/2008/09/19/transparent-jframe-background/ is a hackish way using screenshots and is duplicated in a few other places.

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

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

发布评论

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

评论(2

聚集的泪 2024-12-14 17:20:54

如果不使用 Java v.6u10+ 或调用本机代码的第三方库,您就无法做到这一点。在 Swing Frames 中,对话框和 Windows 被视为“顶级组件”,并在底层窗口系统中呈现。在 Java v6u10 之前,所有顶级组件都有默认的、不透明的浅灰色背景,并且无法从 Java 代码中更改此背景。

You can't do this without using Java v.6u10+ or third-party libraries that call native code. In Swing Frames, Dialogs, and Windows are considered 'top-level components' and are rendered in the underlying windowing system. Before Java v6u10, all top-level components had a default, opaque light-grayish background, and there was no way to change this from Java code.

混吃等死 2024-12-14 17:20:54

叹气我遇到了同样的问题,经过大量时间的研究,我找到了一种在我的计算机上正确设置透明屏幕的方法。然而,一旦将框架设置为透明,很多事情就会停止工作。如果您在任何地方使用 JTextArea 声明,它会立即停止工作。它甚至不会抛出错误!

所以这是代码,希望这有一些用处。如果您用它制作了一个有效的 JScrollPane,请回复:)

public class gui {
    public frame f;
    public String name = "Transparent?"; //name your frame *
    public JButton exit = new JButton("Exit");
    public gui(){ //non-static context
        f = new  JFrame(name);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //*
        f.setLocationRelativeTo(null);
        f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        //maximizes window. if you dont want it, f.setSize(int, int)
        f.setUndecorated(true); //necessary for setOpacity
        f.setOpacity(.7f); //achieve trasparancy
        f.setVisible(true); //show window
        exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                System.exit(0);
            }
        });
        f.add(exit, BorderLayout.CENTER); 
        //since X is not available in the window, make a button to exit
    }

    public static void main(){
        new gui();
    }
}
//* Note: since the default Window manager is set off with setUndecorated(true),
// the name and buttons won't really show. 

Sigh I came across the same problem, and after a lot of time working on it, I found a way to get a properly set transparent screen on my computer. However, a lot of things stop working as soon as you set the frame transparent. If you use JTextArea declaration anywhere, it immediately stop working. It won't even throw an error!

So here's the code, hope this is of some use. Plz reply if you make a working JScrollPane with it :)

public class gui {
    public frame f;
    public String name = "Transparent?"; //name your frame *
    public JButton exit = new JButton("Exit");
    public gui(){ //non-static context
        f = new  JFrame(name);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //*
        f.setLocationRelativeTo(null);
        f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        //maximizes window. if you dont want it, f.setSize(int, int)
        f.setUndecorated(true); //necessary for setOpacity
        f.setOpacity(.7f); //achieve trasparancy
        f.setVisible(true); //show window
        exit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0){
                System.exit(0);
            }
        });
        f.add(exit, BorderLayout.CENTER); 
        //since X is not available in the window, make a button to exit
    }

    public static void main(){
        new gui();
    }
}
//* Note: since the default Window manager is set off with setUndecorated(true),
// the name and buttons won't really show. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文