在Mac OS X上,如何创建透明/半透明的Java JFrame

・2 分钟阅读

我刚刚尝试过用Java快速测试Mac OS X的透明度/半透明度,这个是我用来在Mac OS X10 9上创建透明/半透明Java JFrice的源代码:




package demo;

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;

/**
 * Creates a transparent/translucent Java JFrame on Mac OS X.
 * @author alvin alexander, http://alvinalexander.com
 */
public class MacTranslucentFrame
{
 public static void main(String[] args)
 {
 new MacTranslucentFrame();
 }

 public MacTranslucentFrame()
 {
 SwingUtilities.invokeLater(new Runnable()
 {
 public void run()
 {
 JFrame editorFrame;
 editorFrame = new JFrame(\"Java Mac OS X Translucency Demo\");
 editorFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

 // this is what sets the transparency/translucency on Mac OS X
 editorFrame.getRootPane().putClientProperty(\"Window.alpha\", new Float(0.2f));

 editorFrame.setUndecorated(true);
 editorFrame.setPreferredSize(new Dimension(1200, 900));
 editorFrame.setMinimumSize(new Dimension(1200, 900));
 editorFrame.pack();
 editorFrame.setLocation(10,10);
 editorFrame.setVisible(true);
 }
 });
 }
}


这个代码创建了一个1200 900像素的JavaJFrm,它是80%透明,让你看到你的桌面和它后面的东西。

Xn_warm profile image