2010-07-13 70 views
17

使用Java和Swing,是否有任何(方便)的方式來創建通知? 通過通知,我的意思是這樣的:如何在擺動中創建通知

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

(是否有一個更正確的說法?)。如果它能夠跨平臺運行,那將會很不錯,但我主要關心的是它在Ubuntu下使用Gnome進行工作。如果可能,我想避免在系統托盤/通知區域有一個圖標。

如果一切都失敗,我總是可以使用滑動通知,Sliding Notification bar in java (a la Firefox)

+0

如何使用Libgdx做同樣的事情? – 2016-10-09 15:30:26

回答

26

您可能需要沒有裝飾品的translucent frame

快速演示

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

這是迄今爲止我見過的最好看的東西, ( - : 這太糟糕了,沒有(據我所知)使用原生通知的方式 – Jeremy 2010-07-14 21:27:50

+3

Java本身就支持這個。http://docs.oracle.com/ javase/tutorial/uiswing/misc/trans_shaped_windows.html – kazanaki 2012-08-20 08:37:47

+0

最佳答案。最重要的是,它適用於所有平臺。 – kazy 2015-02-27 13:04:48

14

一個標準的方式來做到這一點是使用Swing TrayIcon API。這可能是最方便的方式:)

+1

猜測,displayMessage將生成第三個屏幕截圖中顯示的樣式。在Windows上,無論如何。 – Powerlord 2010-07-13 19:42:25

+0

在我的系統上,看起來像這樣:http://i.imgur.com/tfTkv.png(我希望能找到更接近原生gnome通知的東西) – Jeremy 2010-07-13 19:54:43

+1

可以創建自定義通知窗口,但它將不那麼「方便」。 基本上,它涉及到使用FocusListener創建一個窗口,以便窗口在失去焦點時關閉。顯然你將能夠做你自己的氣喘吁吁。對於動畫效果,我會建議在http://kenai.com/projects/trident/pages/Home上找到Trident圖書館 – 2010-07-13 20:09:06

4

我還沒有使用過,但JToaster看起來可能是一個很好的匹配。另外,你打算使用SWT嗎?這可能會給你一些額外的選擇(here's an example)。

+0

好點。我可能應該停止使用Swing。 – Jeremy 2010-07-14 11:26:03

+0

@Jeremy,你可能只需要更多的揮杆動作,這是非常強大的(當你知道它,這btw,可能需要一段時間) – OscarRyz 2010-07-14 16:58:20