将鼠标事件传递给Java UI中的应用程序 [英] Pass mouse events to applications behind from a 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中的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!