代码之家  ›  专栏  ›  技术社区  ›  Stephane Grenier

黑色不透明窗口

  •  1
  • Stephane Grenier  · 技术社区  · 14 年前

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

    AWTUtilities.setWindowOpacity(this, 0.5f);
    

    这将使我做的正是我想要的一个例外,颜色是白色的。我怎样才能使颜色变黑?

    setBackground(Color.Black)

    2 回复  |  直到 14 年前
        1
  •  1
  •   Mark Peters    14 年前
            window.getContentPane().setBackground(Color.BLACK);
    
        2
  •  0
  •   user285594 user285594    13 年前

    透明的窗户。

    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);
            }
        }
    
    推荐文章