问题
So I currently have a transparent JFrame that you can click through, but I need it to stay on top of all other windows.
Let's say you have a browser open, I want the JFrame to stay on top of it but be able to catch the keystrokes and mouse clicks.
Here is my current code.
public class TransparentWindow extends JFrame {
private static URL URI;
public TransparentWindow() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
setExtendedState(Frame.MAXIMIZED_BOTH);
setIconImage(Toolkit.getDefaultToolkit().getImage(URI));
setResizable(false);
setUndecorated(true);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setAlwaysOnTop(true);
System.setProperty("sun.java2d.noddraw", "true");
WindowUtils.setWindowTransparent(this, true);
WindowUtils.setWindowAlpha(this, 0.6f);
addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
});
}
public static void main(String[] args) {
try {
URI = new URL("http://i.imgur.com/xtZK0.png");
} catch (MalformedURLException e) {
e.printStackTrace();
}
new TransparentWindow().setVisible(true);
}
}
回答1:
you probably need to do a java process more than a jframe,
jframe can't catch key stroke when not focus, the only way for you to do such a things is to give the focus to you're jframe every time you lost it, meaniing no possible use of any other windows open.
You probably need to change langage.
来源:https://stackoverflow.com/questions/9372362/transparent-click-through-always-on-top-jframe