Java: Making a window click-through (including text/images)

后端 未结 3 561
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 08:52

I want to create an overlay in Java that is transparent, always on top, and that I can click-through. I\'ve found some sim

3条回答
  •  甜味超标
    2020-12-05 09:20

    I tried to make fully "event-transparent" (click-through as you call it) window, but there seems to be some native restrictions on that trick.

    Check this window example:

    public static void main ( String[] args )
    {
        Window w = new Window ( null );
    
        w.add ( new JComponent ()
        {
            protected void paintComponent ( Graphics g )
            {
                g.setColor ( Color.BLACK );
                g.fillRect ( 0, getHeight () / 2 - 10, getWidth (), 20 );
                g.fillRect ( getWidth () / 2 - 10, 0, 20, getHeight () );
            }
    
            public Dimension getPreferredSize ()
            {
                return new Dimension ( 100, 100 );
            }
    
            public boolean contains ( int x, int y )
            {
                return false;
            }
        } );
    
        AWTUtilities.setWindowOpaque ( w, false );
        AWTUtilities.setWindowOpacity ( w, 0.5f );
    
        w.pack ();
        w.setLocationRelativeTo ( null );
        w.setVisible ( true );
    }
    

    Window and component does NOT have any:

    1. Mouse listeners
    2. Mouse motion listeners
    3. Mouse wheel listeners
    4. Key listeners

    Also the component should ignore any kind of mouse events EVEN if there are any listeners due to the modified contains method.

    As you can see - the area where nothing is painted on the component is event-transparent, but the filled area is not. Unluckly i didn't find any workaround to change that behavior. Seems that some "low-level" java methods are blocking the events.

    And this is just a basic JComponent-based example. I don't even say about more complex Swing components like labels, buttons e.t.c. which might have their own event-listeners which could block events.

提交回复
热议问题