我只是想知道是否有任何方法类似于setOpacity(float); 与 JWindow 一起使用。我已经尝试过 setOpacity 方法,但它似乎不适用于 JWindows,因为在尝试使用该方法时出现以下错误
Window 类型的方法 setOpacity(float) 不可见
这是我的主要方法中的代码。
JWindow window = new JWindow();
window.setSize(100.100);
window.setVisible(true);
最简洁的答案是不。但是您还可以做其他事情。
首先看看如何创建半透明和异形窗口
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 下确认操作
更新
试着检查一下是否真的支持半透明......
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);
}