Java透明窗口

我正在尝试创build一个跟随鼠标的圆形窗口,并将点击传递给底层窗口。

我正在用Python和Qt(请参阅Python覆盖窗口 )做这个,但后来我切换到Java和Swing。 但是,我无法使窗口透明。 我试过这种方法,但它不起作用,但我认为我的系统支持透明度,因为如果我启动Screencast-O-Matic (这是在Java中),矩形实际上是透明的。

我怎么能做到这样的事情? (我在Linux KDE4上)

是包含在Python for Windows中的json包吗?

根据文件名select数千个文件

如何调整dynamic加载的本机DLL的%PATH%?

PHP字符编码(Mediawiki中的Umlaute)

每次服务器重新启动后MysqL都会损坏

Windows RT /通用应用程序,文本到语音“保存为MP3”

用于读取input文件的内存映射文件有多安全?

如何将Windows-1251转码为UTF-8?

Windows.h – 焦点input文本input时的通知

将java的结果打印到文件 – >为什么结果文件是空的?

为什么Java教程如何创建半透明和成形的Windows失败? 您使用的是最新版本的Java 6或Java 7吗? 在Java杂志的5月/ 6月期间 ,有一个需要Java 7的形状和透明窗口的教程。您可能需要注册Java杂志才能阅读它。 看看你是否能得到这个在你的系统上运行:

import java.awt.*; //Graphics2D,LinearGradientPaint,Point,Window,Window.Type; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.SwingUtilities; /** * From JavaMagazine May/June 2012 * @author josh */ public class ShapedAboutwindowDemo { /** * @param args the command line arguments */ public static void main(String[] args) { //switch to the right thread SwingUtilities.invokelater(new Runnable() { @Override public void run() { JFrame frame = new JFrame("About Box"); //turn of window decorations frame.setUndecorated(true); //turn off the background frame.setBackground(new Color(0,0)); frame.setContentPane(new AboutComponent()); frame.pack(); //size the window frame.setSize(500,200); frame.setVisible(true); //center on screen frame.setLocationRelativeto(null); } } ); } private static class AboutComponent extends JComponent { public void paintComponent(Graphics graphics) { Graphics2D g = (Graphics2D) graphics; //create a translucent gradient Color[] colors = new Color[]{ new Color(0,0),new Color(0.3f,0.3f,1f),new Color(0,0)}; float[] stops = new float[]{0,0.2f,0.8f,1f}; LinearGradientPaint paint = new LinearGradientPaint( new Point(0,new Point(500,stops,colors); //fill a rect then paint with text g.setPaint(paint); g.fillRect(0,500,200); g.setPaint(Color.WHITE); g.drawString("My Killer App",200,100); } } }

如果您使用Java 6,则需要使用私有API AWTUtilities。 查看Java SE 6 Update 10 API以获取更多详细信息

这是一个很快的黑客攻击,但是这个想法是对的

public class Transparentwindow { /** * @param args the command line arguments */ public static void main(String[] args) { EventQueue.invokelater(new Runnable() { @Override public void run() { MyFrame frame = new MyFrame(); frame.setUndecorated(true); String version = System.getProperty("java.version"); if (version.startsWith("1.7")) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice graphicsDevice = ge.getDefaultScreenDevice(); System.out.println("Transparent from under Java 7"); /* This won't run under Java 6,uncomment if you are using Java 7 System.out.println("isPerPixelAlphaTranslucent = " + graphicsDevice.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT)); System.out.println("isPerPixelAlphaTransparent = " + graphicsDevice.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)); System.out.println("isPerPixelAlphaTranslucent = " + graphicsDevice.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT)); */ frame.setBackground(new Color(0,0)); } else if (version.startsWith("1.6")) { System.out.println("Transparent from under Java 6"); System.out.println("isPerPixelAlphaSupported = " + supportsPerAlphaPixel()); setopaque(frame,false); } frame.setSize(400,400); frame.setLocationRelativeto(null); frame.setVisible(true); } }); } public static class MyFrame extends JFrame { public MyFrame() throws HeadlessException { setContentPane(new MyContentPane()); setDefaultCloSEOperation(EXIT_ON_CLOSE); addMouselistner(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { dispose(); } } }); } } public static class MyContentPane extends JPanel { public MyContentPane() { setLayout(new GridBagLayout()); add(new JLabel("Hello,I'm a transparent frame under Java " + System.getProperty("java.version"))); setopaque(false); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.BLUE); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.5f)); g2d.fillRoundRect(0,getWidth() - 1,getHeight() - 1,20,20); } } public static boolean supportsPerAlphaPixel() { boolean support = false; try { Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); support = true; } catch (Exception exp) { } return support; } public static void setopaque(Window window,boolean opaque) { try { Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); if (awtUtilsClass != null) { Method method = awtUtilsClass.getmethod("setwindowOpaque",Window.class,boolean.class); method.invoke(null,window,opaque); // com.sun.awt.AWTUtilities.setwindowOpaque(this,opaque); // ((JComponent) window.getContentPane()).setopaque(opaque); } } catch (Exception exp) { } } public static void setopacity(Window window,float opacity) { try { Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); if (awtUtilsClass != null) { Method method = awtUtilsClass.getmethod("setwindowOpacity",float.class); method.invoke(null,opacity); } } catch (Exception exp) { exp.printstacktrace(); } } public static float getopacity(Window window) { float opacity = 1f; try { Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities"); if (awtUtilsClass != null) { Method method = awtUtilsClass.getmethod("getwindowOpacity",Window.class); Object value = method.invoke(null,window); if (value != null && value instanceof Float) { opacity = ((Float) value).floatValue(); } } } catch (Exception exp) { exp.printstacktrace(); } return opacity; } }

在Windows 7上生成

在Java 6下

在Java 7下

我想这将工作,我已经尝试过..要使JFrame或窗口透明,你需要先解开Undecorated(true)框架。 这里是示例代码

import javax.swing.*; import com.sun.awt.AWTUtilities; import java.awt.Color; class transFrame { private JFrame f=new JFrame(); private JLabel msg=new JLabel("Hello I'm a Transparent Window"); transFrame() { f.setBounds(400,150,500); f.setLayout(null); f.setUndecorated(true); // Undecorates the Window f.setBackground(new Color(0,25)); // fourth index decides the opacity f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); msg.setBounds(150,250,300,25); f.add(msg); f.setVisible(true); } public static void main(String[] args) { new transFrame(); } }

唯一的问题是你需要添加自己的代码关闭和最小化使用按钮。

如果你想自己做,而不使用外部库,你可以启动一个线程来执行:

设置透明窗口不可见

制作桌面的屏幕截图

把这个截图作为你窗口的背景图片

或者你可以使用JavaFX

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...