简体   繁体   English

制作带有可见边框的透明JFrame

[英]Make transparent JFrame with visible borders

I'm trying to create a transparent JFrame , ie a window where the interior is completely transparent while the border, which is the top bar with the close button, minimize etc visible. 我正在尝试创建一个透明的JFrame ,即内部完全透明的窗口,而边框(带有关闭按钮的顶部栏)的边框最小化,等可见。 I tried creating a new Jpanel and then using panel.setOpaque(false); 我尝试创建一个新的Jpanel ,然后使用panel.setOpaque(false); , but it did not help. ,但没有帮助。

I'm extremely new to swing and Java GUI and would love to get some help. 我是Swing和Java GUI的新手,很想获得帮助。

What you want to do is possible, but not in the way you're imagining it... 您想做的事是可能的,但并非以您想象的方式...

If you want your standard operating system's window decorations (ie window border, close, maximize, minimize buttons), it's impossible. 如果要使用标准操作系统的窗口装饰(例如,窗口边框,关闭,最大化,最小化按钮),则不可能。 Transparency cannot be applied to 'decorated' frames. 透明度不能应用于“装饰”帧。

However, this can be done by using Java's default window decorations. 但是,这可以通过使用Java的默认窗口装饰来完成。 Try out the code below: 试试下面的代码:

public static void main(String[] args) {
    JPanel panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            g.setColor(Color.RED);
            g.drawRect(32,32,32,32);
        }
    };

    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame f = new JFrame();
    f.add(panel);
    f.setSize(256,256);

    // The following two statements cause the window-transparency:
    f.setUndecorated(true);
    f.setBackground(new Color(0,255,0,0));

    f.setVisible(true);
}

I personally don't find the Java window decorations very nice looking... Apart from that, OS-specific features (such as Window's Snap feature by 'half-maximizing' a window by moving it to the side of the screen) don't work... To me this is a deal-breaker, but depending on your needs, it could be an acceptable solution. 我个人并不觉得Java窗口装饰看起来很漂亮...除此之外,特定于操作系统的功能(例如,通过将窗口移动到屏幕侧面以“半最大化”窗口的Window的Snap功能)不会”对我来说,这是个大难题,但根据您的需要,它可能是一个可以接受的解决方案。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM