Creating a JFrame you can click through

前端 未结 1 1148
梦毁少年i
梦毁少年i 2020-12-18 08:06

I\'m trying to create a jframe that the user can click through. I\'m not looking for opacity but transparency.

I need a solution that works on all OS

相关标签:
1条回答
  • 2020-12-18 08:47

    A completely transparent window can be achieved in Java 7 by using a completely transparent background color, for example...

    JFrame frame = new JFrame("Testing");
    frame.setUndecorated(true);
    frame.setBackground(new Color(0, 0, 0, 0));
    

    The problem you will have is, that anywhere there is any kind of solid pixel (even if it is transparent), it will stop the mouse events from going beyond the window...

    Transparent

    This means that every child component you add to the frame (that you want to be able to click through) will need to be transparent as well.

    I use a similar technique for some of my utility programs and include a MouseListener on the main opaque component to make the window more visible so I can drag it if I want to it, but that's stuff for another question ;)

    import java.awt.AlphaComposite;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class ClickThroughWindow {
    
        public static void main(String[] args) {
            new ClickThroughWindow();
        }
    
        public ClickThroughWindow() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setUndecorated(true);
                    frame.setBackground(new Color(0, 0, 0, 0));
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.setAlwaysOnTop(true);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setOpaque(false);
                setLayout(new GridBagLayout());
                add(new JLabel("Hello"));
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setColor(getBackground());
    //            g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
    //            g2d.fillRect(0, 0, getWidth(), getHeight());
                g2d.setColor(Color.BLACK);
                g2d.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
                g2d.dispose();
            }
    
        }
    
    }
    

    You can find more details at How to Create Translucent and Shaped Windows, in particular How to Implement Per-Pixel Translucency

    0 讨论(0)
提交回复
热议问题