JWindow黑色不透明度

时间:2010-06-18 20:11:03

标签: java swing opacity jdialog jwindow

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

例如,如果我写:

AWTUtilities.setWindowOpacity(this, 0.5f);

这将完全符合我的要求,但有一个例外,颜色为白色。如何让颜色变黑?

我已经尝试了setBackground(Color.Black)等所有关于“这个”的所有内容......

2 个答案:

答案 0 :(得分:2)

        window.getContentPane().setBackground(Color.BLACK);

答案 1 :(得分:0)

透明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);
        }
    }