将鼠标事件传递给Java UI中的应用程序

编程入门 行业动态 更新时间:2024-10-27 17:13:29
本文介绍了将鼠标事件传递给Java UI中的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的问题与如何将鼠标事件传递给C#/ Vista中的我的应用程序?,但是我需要一个透明的Java UI。我可以使用6.0轻松创建一个透明的Java UI,但无法获取有关将应用程序传递给任何应用程序(比如浏览器)的信息。

The question I have is exactly same in requirement as How to pass mouse events to applications behind mine in C#/Vista? , but I need the same for a Transparent Java UI. I can easily create a transparent Java UI using 6.0 but couldn't get any info about passing events through the app to any applications(say a browser) behind.

推荐答案

我相信这会回答你的问题。要运行它,您将需要Java 6更新10及更高版本。 我在Windows Vista上测试了

I believe this will answer your question. To run it you will need Java 6 update 10 and above. I tested it on Windows Vista

import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JFrame; import javax.swing.JPanel; public class ClickThrough { public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JFrame f = new JFrame("Test"); f.setAlwaysOnTop(true); Component c = new JPanel() { @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g.create(); g2.setColor(Color.gray); int w = getWidth(); int h = getHeight(); g2.fillRect(0, 0, w,h); g2.setComposite(AlphaComposite.Clear); g2.fillRect(w/4, h/4, w-2*(w/4), h-2*(h/4)); } }; c.setPreferredSize(new Dimension(300, 300)); f.getContentPane().add(c); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); com.sun.awt.AWTUtilities.setWindowOpaque(f,false); } }

请注意,您需要一个未装饰的窗口或由Java单独装饰的窗口(而不是默认的OS装饰),否则代码将不起作用。

Note that you need to either have an undecorated window or one that is decorated by Java alone (not the default OS decoration) otherwise the code won't work.

更多推荐

将鼠标事件传递给Java UI中的应用程序

本文发布于:2023-11-16 07:47:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1602364.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   应用程序   事件   Java   UI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!