如何在秋千中创建通知 [英] How to create a notification in swing

查看:115
本文介绍了如何在秋千中创建通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java和Swing,是否有任何(方便)方法来创建通知?
通知我的意思是:



解决方案

您可能需要一个没有装饰的半透明框架



快速演示



OSX



]



您可以利用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;


/ **
*关于半透明窗口
*如何用于显示系统时钟的简单演示。
* @author< a href =http://stackoverflow.com/users/20654/oscarryz> Oscar Reyes< / a>
* /
class Translucent extends JPanel实现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;
}

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 =新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);
}
//取自: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);
方法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();
}
}
}


Using Java and Swing, is there any (convenient) way to create a notification? By notification, I mean something like:

this http://productivelinux.com/images/notify-osd-screenshot.png, this http://images.maketecheasier.com/2010/01/kfirefox-notify-indexed.png, or

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

If all else fails, I could always use the sliding notification from Sliding Notification bar in java (a la Firefox)

解决方案

You might need a translucent frame without decorations.

Quick demo

OSX

]

You can take advantage JLabel displays simple 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();
            }
    }
}

这篇关于如何在秋千中创建通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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