如何在Swing中创建通知

21
使用Java和Swing,有没有创建通知(便捷)的方法? 所谓通知,是指像这样的东西:

thisthis
(来源:maketecheasier.com
, 或者 this
(来源:microsoft.com

(这个东西有更正确的术语吗?)最好能够跨平台工作,但我主要关心它在Ubuntu的Gnome下是否工作。如果可能的话,我想避免在系统托盘/通知区域中拥有图标。

如果一切都失败了,我可以使用来自 Java中的滑动通知栏(类似Firefox)的滑动通知。


如何使用Libgdx实现相同的功能? - salih kallai
4个回答

29

你可能需要一个没有装饰的半透明窗口

快速演示

OSX

enter image description here

你可以利用JLabel展示简单的HTML

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.Date;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;


/**
 * Simple demo on how a translucent window
 * looks like when is used to display the system clock.
 * @author <a href="http://stackoverflow.com/users/20654/oscarryz">Oscar Reyes</a>
 */
class Translucent extends JPanel implements ActionListener {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
    private final Date now = new Date();
    private final Timer timer = new Timer(1000, this);
    private final JLabel text = new JLabel();

    public Translucent() {
        super(true);
        timer.start();
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        now.setTime(System.currentTimeMillis());
        text.setText(String.format("<html><body><font size='50'>%s</font></body></html>",sdf.format(now)));
    }

    public static void main(String[] args) {

        JFrame f = new JFrame();
        f.setUndecorated(true);
        setTranslucency(f);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBackground(new Color(0f, 0f, 0f, 1f / 3f));
        JPanel p =  new Translucent();
        JLabel l = new JLabel("Hola");
        l.setFont(new Font(l.getFont().getName(), Font.PLAIN, 128));
        p.add(l);
        f.add(p);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
    // taken from: http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/
    private static void setTranslucency( Window window){
        try {
               Class<?> awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities");
               Method mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class);
               if (!mSetWindowOpacity.isAccessible()) {
                   mSetWindowOpacity.setAccessible(true);
               }
               mSetWindowOpacity.invoke(null, window, Float.valueOf(0.75f));
            } catch (NoSuchMethodException ex) {
               ex.printStackTrace();
            } catch (SecurityException ex) {
               ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
               ex.printStackTrace();
            } catch (IllegalAccessException ex) {
               ex.printStackTrace();
            } catch (IllegalArgumentException ex) {
               ex.printStackTrace();
            } catch (InvocationTargetException ex) {
               ex.printStackTrace();
            }
    }
}

这是我迄今为止看到的最漂亮的东西,所以我会选择它 (-: 很遗憾(就我所知),没有办法使用swing的本地通知。 - Jeremy
4
Java 现在原生支持此功能。http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html - kazanaki
1
损坏的链接 @OscarRyz - omushpapa

15

1
猜测displayMessage将会生成第三个屏幕截图中所示的样式。在Windows上,无论如何。 - Powerlord
1
在我的系统上,它看起来像这样:http://i.imgur.com/tfTkv.png(我希望它更接近原生的gnome通知)。 - Jeremy
1
可以创建自定义通知窗口,但是这可能会稍微不那么“方便”。基本上,它涉及创建具有FocusListener的窗口,以便在失去焦点时关闭窗口。显然,您将能够进行自己的绘画。对于动画效果,我建议使用在http://kenai.com/projects/trident/pages/Home找到的Trident库。 - Eugene Ryzhikov

5

我没有使用过这个,但JToaster看起来可能是一个很好的选择。此外,你是否愿意使用SWT?这可能会给你一些额外的选择(这里有一个例子)。


@Jeremy,你可能只需要更多地使用swing,它非常强大(当你熟悉它时,这可能需要一段时间)。 - OscarRyz

2

它已经不存在了 - Queeg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接