创建一个可以点击的 JFrame [英] Creating a JFrame you can click through

查看:24
本文介绍了创建一个可以点击的 JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个用户可以点击的 jframe.我不是在寻找不透明度而是透明度.

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

我需要一个适用于所有操作系统而不仅仅是 Windows 的解决方案,因为我无法使用

I need a solution that works on all OS and not just Windows because I can't use

WindowUtils.setWindowTransparent(frame, true);
WindowUtils.setWindowAlpha(frame, 0.6f);

AWTUtilities.setWindowOpaque(this, false);
AWTUtilities.setWindowOpacity(this, 0.8f);

我可以单独用 java 来完成这个吗?如果有我必须使用的库就好了.

Can I accomplish this with java alone? It's ok if there is a library that I must use.

我的 jframe 没有装饰,这里是它的代码.

I have my jframe undecorated and here is the code for it.

frame = new JDialog();
frame.setUndecorated(true);
frame.setVisible(true);
frame.setOpacity(Shared.opacity);
frame.setLocation(0, 0);
frame.setSize(Shared.screenWidth, Shared.screenHeight);

当我说用户可以点击时,我的意思是,如果我的框架在顶部,但他们在我的下方有一个窗口,点击我的将把一个窗口带到顶部.>

When I say the user can click through what I mean is that if my frame is on top but they have a window under mine, clicking on mine would bring the one under on top.

推荐答案

Java 7 中可以通过使用完全透明的背景色来实现完全透明的窗口,例如...

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...

这意味着您添加到框架中的每个子组件(您希望能够点击)也需要是透明的.

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.

我对我的一些实用程序使用了类似的技术,并在主不透明组件上包含一个 MouseListener 以使窗口更加可见,以便我可以根据需要拖动它,但就是这样另一个问题;)

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();
        }

    }

}

您可以在如何创建半透明和异形窗口中找到更多详细信息,特别是 如何实现 Per-像素半透明

这篇关于创建一个可以点击的 JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆