什么可以用来提示半透明窗口或通知?屏幕截图示例已附加 [英] what can be used to prompt translucent window or notification? screenshot example attacehed

查看:116
本文介绍了什么可以用来提示半透明窗口或通知?屏幕截图示例已附加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用什么工具来重新创建类似的半透明窗口?

What tool can be used to recreate similar translucent window?

http://cdn.osxdaily.com/wp-content/uploads/2013/05/bluetooth-connected-connection-lost-mac.jpg

我今天尝试的是一个演示示例:从Oracle uiswing trans_shaped_windows 但我收到无法启动该应用程序."

What I tried today is demo example of: uiswing trans_shaped_windows from Oracle but I'm getting "Unable to launch the application."

但是我得到一个错误,即oracle页面不存在,所以我无法检查并确认它是否可以按我的意愿工作.

But I got the error that oracle page doesn't exist so I'm not able to check and confirm that it works as I wanted.

com.sun.deploy.net.FailedDownloadException: Unable to load resource: https://docs.oracle.com/javase/tutorialJWS/samples/uiswing/TranslucentWindowDemoProject/TranslucentWindowDemo.jar
    at com.sun.deploy.net.DownloadEngine.actionDownload(Unknown Source)
    at com.sun.deploy.net.DownloadEngine.downloadResource(Unknown Source)
    at com.sun.deploy.cache.ResourceProviderImpl.getResource(Unknown Source)
    at com.sun.deploy.cache.ResourceProviderImpl.getResource(Unknown Source)
    at com.sun.javaws.LaunchDownload$DownloadTask.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

因此,如果您尝试使用类似的窗口或对如何找到所需的库和示例有任何想法,请对此发表评论.

So if you tried to use similar window or have some ideas how to find required libraries and example please comment on this.

推荐答案

有一个小窍门...

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

现在,这将创建一个完全透明的窗口,而不是您要说的内容,但是窍门是,在窗口内放置一个组件,然后可以绘制您想要的形状,例如...

Now, this will make a completely transparent window, not what you want you say, but the trick is, to place a component within the window which can then paint the shape you want, for example...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Popup {

    public static void main(String[] args) {
        new Popup();
    }

    public Popup() {
        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.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setOpaque(false);
            setLayout(new GridBagLayout());
            setBorder(new EmptyBorder(20, 20, 20, 20));
            try {
                add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResource("Windows10.png")))));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_DEFAULT);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);
            g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_DEFAULT);
            g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DEFAULT);
            g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_DEFAULT);
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_DEFAULT);
            g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
            g2d.setColor(new Color(128, 128, 128, 128));
            g2d.fill(new RoundRectangle2D.Float(0, 0, getWidth() - 1, getHeight() - 1, 20, 20));
            g2d.dispose();
        }

    }

}

看看如何创建半透明和异形的窗口执行自定义绘画 查看全文

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