代码之家  ›  专栏  ›  技术社区  ›  Martijn Courteaux

Java:透明窗口和非透明组件?

  •  4
  • Martijn Courteaux  · 技术社区  · 15 年前

    我刚见到公用事业公司( com.sun.awt.AWTUtilities )让你的JFrame 真正地 here . 这个效果很好。即使在Linux中,打开了不稳定窗口的桌面效果也是如此。但是我想在透明的JFrame上做一个非透明的组件。

    以下是我使用的代码:

    import com.sun.awt.AWTUtilities;
    
    /* "this" is the JFrame */
    this.setUndecorated(true);
    AWTUtilities.setWindowOpaque(this, true);
    AWTUtilities.setWindowOpacity(this, 0.5f);
    AWTUtilities.setWindowShape(this, new RoundRectangle2D.Float(0f, 0f, (float) getWidth(), (float) getHeight(), 15f, 15f));
    
    2 回复  |  直到 6 年前
        1
  •  4
  •   Community CDub    8 年前

    Translucent Windows 适用于整个 java.awt.Window 和内容,但您可以尝试下面和本文中所示的方法 example .

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setBackground(new Color(0f, 0f, 0f, 0.1f));
    f.setUndecorated(true);
    f.add(new JLabel("<html>Testing<br>1, 2, 3</html>"));
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
    
        2
  •  1
  •   qmega    15 年前

    setWindowOpaque false 并绘制低alpha(或0 alpha,如果你想完全透明)的背景。组件仍将绘制为不透明。看到了吗 this article 再看看 每像素半透明性 .