- 浏览: 44260 次
- 性别:
- 来自: 深圳
-
文章分类
最新评论
引用http://www.iteye.com/topic/841846
package com.msg;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TextArea;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel;
/**
*
* 创建闪动的托盘图像
* @author Everest
*
*/
public class BickerTray extends JFrame implements Runnable {
private static final long serialVersionUID = -3115128552716619277L;
private SystemTray sysTray;// 当前操作系统的托盘对象
private TrayIcon trayIcon;// 当前对象的托盘
private ImageIcon icon = null;
private TextArea ta = null;
private static int count = 1; //记录消息闪动的次数
private boolean flag = false; //是否有新消息
private static int times = 1; //接收消息次数
public BickerTray() {
this.createTrayIcon();// 创建托盘对象
Image image = this.getToolkit().getImage(getRes("com/img/f32.gif"));
this.setIconImage(image);
init();
}
public URL getRes(String str){
return this.getClass().getClassLoader().getResource(str);
}
/**
* 初始化窗体的方法
*/
public void init() {
this.setTitle("消息盒子");
ta = new TextArea("");
ta.setEditable(false);
this.add(ta);
this.setSize(400, 400);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
// 添加窗口最小化事件,将托盘添加到操作系统的托盘
/*this.addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
addTrayIcon();
}
});*/
addTrayIcon();
this.setVisible(true);
}
/**
* 添加托盘的方法
*/
public void addTrayIcon() {
try {
sysTray.add(trayIcon);// 将托盘添加到操作系统的托盘
setVisible(false); // 使得当前的窗口隐藏
new Thread(this).start();
} catch (AWTException e1) {
e1.printStackTrace();
}
}
/**
* 创建系统托盘的对象 步骤:
* 1,获得当前操作系统的托盘对象
* 2,创建弹出菜单popupMenu
* 3,创建托盘图标icon
* 4,创建系统的托盘对象trayIcon
*/
public void createTrayIcon() {
sysTray = SystemTray.getSystemTray();// 获得当前操作系统的托盘对象
icon = new ImageIcon(getRes("com/img/f17.gif"));// 托盘图标
PopupMenu popupMenu = new PopupMenu();// 弹出菜单
MenuItem mi = new MenuItem("打开");
MenuItem exit = new MenuItem("退出");
popupMenu.add(mi);
popupMenu.add(exit);
// 为弹出菜单项添加事件
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setText(ta.getText()+"\n==============================================\n 《通知》 今天下午4:00到大礼堂开会。 \n 第"+times+"次接收时间:"+ new Date().toLocaleString()); // 设置通知消息内容
BickerTray.this.setExtendedState(JFrame.NORMAL);
BickerTray.this.setVisible(true); // 显示窗口
BickerTray.this.toFront(); //显示窗口到最前端
flag = false; //消息打开了
count = 0; times++;
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
trayIcon = new TrayIcon(icon.getImage(), "消息盒子", popupMenu);
/** 添加鼠标监听器,当鼠标在托盘图标上双击时,默认显示窗口 */
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) { // 鼠标双击
ta.setText(ta.getText()+"\n==============================================\n 《通知》 今天下午4:00到大礼堂开会。 \n 第"+times+"次接收时间:"+ new Date().toLocaleString()); // 设置通知消息内容
BickerTray.this.setExtendedState(JFrame.NORMAL);
BickerTray.this.setVisible(true); // 显示窗口
BickerTray.this.toFront();
flag = false; //消息打开了
count = 0; times++;
}
}
});
}
/**
* 线程控制闪动
*/
public void run() {
while (true) {
if(flag){ // 有新消息
try {
if(count == 1){
// 播放消息提示音
//AudioPlayer p = new AudioPlayer(getRes("file:com/sound/Msg.wav"));
//p.play(); p.stop();
try {
AudioClip p = Applet.newAudioClip(new URL("file:sound/msg.wav"));
p.play();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
// 闪动消息的空白时间
this.trayIcon.setImage(new ImageIcon("").getImage());
Thread.sleep(500);
// 闪动消息的提示图片
this.trayIcon.setImage(icon.getImage());
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
count++;
}else{ // 无消息或是消息已经打开过
this.trayIcon.setImage(icon.getImage());
try {
Thread.sleep(20000);
flag = true;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
try {
UIManager.setLookAndFeel(new SubstanceBusinessBlueSteelLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new BickerTray();
}
});
}
}
package com.msg;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TextArea;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel;
/**
*
* 创建闪动的托盘图像
* @author Everest
*
*/
public class BickerTray extends JFrame implements Runnable {
private static final long serialVersionUID = -3115128552716619277L;
private SystemTray sysTray;// 当前操作系统的托盘对象
private TrayIcon trayIcon;// 当前对象的托盘
private ImageIcon icon = null;
private TextArea ta = null;
private static int count = 1; //记录消息闪动的次数
private boolean flag = false; //是否有新消息
private static int times = 1; //接收消息次数
public BickerTray() {
this.createTrayIcon();// 创建托盘对象
Image image = this.getToolkit().getImage(getRes("com/img/f32.gif"));
this.setIconImage(image);
init();
}
public URL getRes(String str){
return this.getClass().getClassLoader().getResource(str);
}
/**
* 初始化窗体的方法
*/
public void init() {
this.setTitle("消息盒子");
ta = new TextArea("");
ta.setEditable(false);
this.add(ta);
this.setSize(400, 400);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
// 添加窗口最小化事件,将托盘添加到操作系统的托盘
/*this.addWindowListener(new WindowAdapter() {
public void windowIconified(WindowEvent e) {
addTrayIcon();
}
});*/
addTrayIcon();
this.setVisible(true);
}
/**
* 添加托盘的方法
*/
public void addTrayIcon() {
try {
sysTray.add(trayIcon);// 将托盘添加到操作系统的托盘
setVisible(false); // 使得当前的窗口隐藏
new Thread(this).start();
} catch (AWTException e1) {
e1.printStackTrace();
}
}
/**
* 创建系统托盘的对象 步骤:
* 1,获得当前操作系统的托盘对象
* 2,创建弹出菜单popupMenu
* 3,创建托盘图标icon
* 4,创建系统的托盘对象trayIcon
*/
public void createTrayIcon() {
sysTray = SystemTray.getSystemTray();// 获得当前操作系统的托盘对象
icon = new ImageIcon(getRes("com/img/f17.gif"));// 托盘图标
PopupMenu popupMenu = new PopupMenu();// 弹出菜单
MenuItem mi = new MenuItem("打开");
MenuItem exit = new MenuItem("退出");
popupMenu.add(mi);
popupMenu.add(exit);
// 为弹出菜单项添加事件
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setText(ta.getText()+"\n==============================================\n 《通知》 今天下午4:00到大礼堂开会。 \n 第"+times+"次接收时间:"+ new Date().toLocaleString()); // 设置通知消息内容
BickerTray.this.setExtendedState(JFrame.NORMAL);
BickerTray.this.setVisible(true); // 显示窗口
BickerTray.this.toFront(); //显示窗口到最前端
flag = false; //消息打开了
count = 0; times++;
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
trayIcon = new TrayIcon(icon.getImage(), "消息盒子", popupMenu);
/** 添加鼠标监听器,当鼠标在托盘图标上双击时,默认显示窗口 */
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) { // 鼠标双击
ta.setText(ta.getText()+"\n==============================================\n 《通知》 今天下午4:00到大礼堂开会。 \n 第"+times+"次接收时间:"+ new Date().toLocaleString()); // 设置通知消息内容
BickerTray.this.setExtendedState(JFrame.NORMAL);
BickerTray.this.setVisible(true); // 显示窗口
BickerTray.this.toFront();
flag = false; //消息打开了
count = 0; times++;
}
}
});
}
/**
* 线程控制闪动
*/
public void run() {
while (true) {
if(flag){ // 有新消息
try {
if(count == 1){
// 播放消息提示音
//AudioPlayer p = new AudioPlayer(getRes("file:com/sound/Msg.wav"));
//p.play(); p.stop();
try {
AudioClip p = Applet.newAudioClip(new URL("file:sound/msg.wav"));
p.play();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
// 闪动消息的空白时间
this.trayIcon.setImage(new ImageIcon("").getImage());
Thread.sleep(500);
// 闪动消息的提示图片
this.trayIcon.setImage(icon.getImage());
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
count++;
}else{ // 无消息或是消息已经打开过
this.trayIcon.setImage(icon.getImage());
try {
Thread.sleep(20000);
flag = true;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
try {
UIManager.setLookAndFeel(new SubstanceBusinessBlueSteelLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new BickerTray();
}
});
}
}
- 73328cc9-2339-3dda-88f0-a828cb6e84db.rar (3.3 MB)
- 下载次数: 11
发表评论
-
java注解
2019-03-27 13:42 0https://blog.csdn.net/mrs_wu/a ... -
jsonp原理
2019-03-26 15:39 0https://blog.csdn.net/hansexpl ... -
BufferedImage在linux下createGraphics卡住的问题
2019-02-28 17:30 0BufferedImage在linux下createGra ... -
记第二次面试(平安)
2019-02-21 22:31 01.JVM调优 2.线程池,几种线程的帮助类 3.Has ... -
ueditor处理webp图片
2019-02-28 17:26 680今天运营MM突然说,资讯管理功能用不了,从 ... -
记一次面试(金地)
2019-01-28 09:40 0首先是笔试部分: 1. ... -
java io
2018-11-28 23:43 0https://www.cnblogs.com/hoojj ... -
Java 集合详解
2018-11-26 15:27 0https://www.cnblogs.com/ysocean ... -
Redis分布式锁的正确实现方式
2018-10-06 10:44 517分布式锁一般有三种实现方式: 1. 数据库乐观锁; 2 ... -
在 CentOS7 上安装 Tomcat9
2018-01-30 19:25 3616 下载 apache-tomcat-9.0.0.M4.t ... -
在CentOS7上安装JDK1.8
2018-01-30 18:50 3701 通过 SecureCRT 连接到阿里云 CentOS7 ... -
centos7自定义安装redis
2017-12-18 16:26 5391.下载 wget http://download.re ... -
centos7自定义安装mysql5.7
2017-12-15 18:12 4781.查找卸载原有的mysql 方法1 rpm -qa| ... -
centos7 安装nginx
2017-12-13 18:18 3291.解压 tar -xf nginx-1.10.1.ta ... -
mysqldump 每日定时备份
2017-09-25 19:37 615首先编写脚本: date_cur=$(date &quo ... -
centos7 安装nginx
2017-05-15 16:07 01、下载nginx-release包 以CentOS 7为 ... -
读书笔记
2017-04-18 11:31 02017-04-18 1.程序员,这 ... -
centos7下mongodb的安装与配置
2017-04-13 16:59 6971.下载mongodb安装包 https://www.m ... -
服务器迁移心得
2016-11-04 15:22 4891.数据传输直接从源到目标,不要经过自己的电脑,用到的li ... -
svn lock
2016-09-13 10:33 448在网上的解决方案中,往往都是通过Clean up来解决,如下 ...
相关推荐
总之,托盘闪动消息提醒是提高用户体验的重要工具,而Java的`TrayIcon`类为此提供了便利。结合声音提示,这种提醒方式能确保用户不会错过任何关键信息,从而提升软件的实用性。通过分析和实践这个JAVA版的示例,...
用户上线后自动加载用户信息,用户修改头像后其他用户也会自动加载更新,消息发送过来头像会跳动,当最小化到托盘后,托盘图标也会闪动。用户间可以传递文件,发送消息。服务端可以向每个用户发送消息 可以直接运行...
"仿QQ状态栏消息闪动"是一个典型的用户通知功能,常见于即时通讯软件如QQ,用来提醒用户有未读消息或新活动。这个功能在Windows系统中通常表现为任务栏图标或通知区域图标闪烁,吸引用户的注意力。下面我们将详细...
- 在Java中,可以调用`TrayIcon.displayMessage()`方法,设置消息类型为` TrayIcon.MessageType.INFO`或`TrayIcon.MessageType.WARNING`,配合`TrayIcon.setToolTip()`设置提示信息,来达到闪动效果。 - .NET ...
同时,为了提高用户体验,开发者还可能需要考虑到消息气泡的动画效果、可定制的设置以及用户对消息的确认和关闭选项。 总的来说,trayicon弹出消息气泡是一种高效、便捷的用户通知机制,它在各种软件和系统中广泛...
8. **消息处理**:消息链表用于保存用户接收的各种消息,当有新消息到达或好友上线时,系统会通过闪动托盘图标或播放音乐的方式提醒用户。 9. **文件系统设计**:聊天记录和用户号码的存储采用了文件系统,这有利于...
标题“SysTray QQ”指的是一个利用Java编程语言中的特定功能来实现类似QQ通知效果的应用。这个应用主要涉及了JDK 1.6版本之后引入的`SysTray`、`TrayIcon`以及`Desktop`这三个核心类,它们是Java提供给开发者用于...
(14)托盘闪动:跟QQ完全一样,当接收到消息时,托盘会闪动对应好友的头像。点击头像,将弹出与好友的聊天框。 (15)最近联系人列表 (16)系统设置:开机自动启动、麦克风设备索引、摄像头设备索引,叉掉主窗口时...