Java 如何在swing中创建通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3240415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a notification in swing
提问by Jeremy
Using Java and Swing, is there any (convenient) way to create a notification? By notification, I mean something like:
使用 Java 和 Swing,是否有任何(方便的)方法来创建通知?通过通知,我的意思是:
,
(source: maketecheasier.com)
,
or
(source: microsoft.com)
,(来源:maketecheasier.com),或
(来源:microsoft.com)
(Is there a more correct term for that?). It would be nice if it worked cross-platform, but I'm mainly concerned with it working under Ubuntu with Gnome. If at all possible, I would like to avoid having an icon in the system tray / notification area.
(有没有更正确的术语?)。如果它跨平台工作会很好,但我主要关心它在 Ubuntu 和 Gnome 下工作。如果可能的话,我想避免在系统托盘/通知区域中有一个图标。
If all else fails, I could always use the sliding notification from Sliding Notification bar in java (a la Firefox)
如果所有其他方法都失败了,我总是可以使用java 中的滑动通知栏的滑动通知(a la Firefox)
采纳答案by OscarRyz
You might need a translucent framewithout decorations.
您可能需要一个没有装饰的半透明框架。
Quick demo
快速演示
OSX
操作系统
]
]
You can take advantage JLabel displays simple HTML
您可以利用 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();
}
}
}
回答by Eugene Ryzhikov
A standard way to do it is to use Swing TrayIcon API. That would probably be the most convenient way too :)
一种标准的方法是使用Swing TrayIcon API。这也可能是最方便的方式:)
回答by Matt Solnit
I haven't used this, but JToasterlooks like it might be a good match. Also, are you open to using SWT? This might give you some additional options (here's an example).
我没有使用过这个,但JToaster看起来可能是一个很好的匹配。另外,您愿意使用 SWT 吗?这可能会给你一些额外的选择(这里有一个例子)。
回答by Mr Coder
I highly recommend following library , it provides normal JFrame inside notification which gives full control .
我强烈推荐以下库,它提供了正常的 JFrame 内部通知,可以完全控制。