任何与JWindow一起使用的setOpacity方法

时间:2013-10-17 01:23:47

标签: java swing frame jwindow

我只是想知道是否有任何类似 setOpacity(float); 的方法与JWindow一起工作。我已经尝试了setOpacity方法,但似乎它不适用于JWindows,因为我在尝试使用方法时遇到以下错误

  

来自类型Window的方法setOpacity(float)不可见

以下是我的主要方法中的代码。

JWindow window = new JWindow();
window.setSize(100.100);
window.setVisible(true);

1 个答案:

答案 0 :(得分:0)

简短的回答是否定的。但是你还可以做其他事情。

首先看一下How to Create Translucent and Shaped Windows

enter image description here

public class TranslucentWindow {

    public static void main(String[] args) {
        new TranslucentWindow();
    }

    public TranslucentWindow() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JWindow window = new JWindow();
                window.setSize(100, 100);
                window.setBackground(new Color(255, 0, 0, 128));
                window.setLocationRelativeTo(null);
                window.setVisible(true);

            }
        });
    }

}

<强>更新

Windows 7和MacOS X,Java 7下的确认操作

enter image description here

<强>更新

尝试并检查是否确实支持半透明......

GraphicsEnvironment ge
        = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();

boolean isUniformTranslucencySupported
        = gd.isWindowTranslucencySupported(TRANSLUCENT);
boolean isPerPixelTranslucencySupported
        = gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
boolean isShapedWindowSupported
        = gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT);

System.out.println("Hello");

if (isUniformTranslucencySupported && isPerPixelTranslucencySupported && isShapedWindowSupported) {

    // Build window as normal...

} else {

    if (!isUniformTranslucencySupported) {
        System.err.println("Translucentancy is not support");
    }
    if (!isPerPixelTranslucencySupported) {
        System.err.println("Per Pixel Translucentancy is not support");
    }
    if (!isShapedWindowSupported) {
        System.err.println("Per Pixel Transparenancy is not support");
    }

    System.exit(0);

}