用Java做一个聊天程序,在任务栏处提示效果怎么做?
我用java做了一个聊天小程序,现在想给它加一个效果,就是当点最好化按钮把窗口压到任务栏时,有消息来的时候任务栏的“最小化”会有提示,比如像QQ一样,会变颜色或变亮。请这...
我用java做了一个聊天小程序,现在想给它加一个效果,就是当点最好化按钮把窗口压到任务栏时,有消息来的时候任务栏的“最小化”会有提示,比如像QQ一样,会变颜色或变亮。请这个怎么做啊?
展开
3个回答
展开全部
java 可以实现将程序最小化到托盘的吧.....
甚至还可以设定最小化到托盘的图标,这样的话,当有新消息到达时,你可以把它的图标设置成无色的另一图片(或者只将其最小化,而不设置其图标),然后再设置成原来的图片,如此反复,就实现了闪烁的功能.........
不过似乎先要判断当前操作系统是否支持系统托盘..........下面这个程序实现了系统托盘,但是似乎在我的电脑上没实现出来,难道操作系统不支持?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SystemTrayTest extends JFrame implements ActionListener
{
//创建菜单、菜单项数组、消息类型
PopupMenu popup=new PopupMenu();
Menu menu=new Menu("消息类型");
MenuItem[] itemArray ={new MenuItem("信息消息"),new MenuItem("常规消息"),
new MenuItem("警告消息"),new MenuItem("错误消息"),new MenuItem("退出程序")};
//定义系统托盘、托盘图标变量
SystemTray tray;
TrayIcon trayIcon;
//构造方法
public SystemTrayTest(){
//对菜单项添加监听并将菜单项添加到菜单中
for(int i=0;i<itemArray.length;i++){
if(i<4){
itemArray[i].addActionListener(this);//为菜单项注册监听器
//将菜单项数组中前3个菜单项添加进"弹出消息"菜单中
menu.add(itemArray[i]);
}
itemArray[4].addActionListener(this);//添加监听
popup.add(menu);//将弹出消息菜单添加到菜单中
popup.add(itemArray[4]);//将退出菜单添加到菜单中
}
// 判断当前操作系统是否支持系统托盘
if (SystemTray.isSupported()) {
//获取系统托盘
tray = SystemTray.getSystemTray();
//加载图标
Image image = Toolkit.getDefaultToolkit().getImage("d:/trayIcon.jpg");
//创建托盘图标
trayIcon=new TrayIcon(image,"系统托盘测试",popup);
//托盘图标自动设置尺寸
trayIcon.setImageAutoSize(true);
try{//添加托盘图标到系统托盘中
tray.add(trayIcon);
}
catch(AWTException e){
e.printStackTrace();
}
//为托盘图标注册监听器
trayIcon.addActionListener(this);
}
//设置窗体关闭按扭所执行的动作
this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
SystemTrayTest.this.hide();//隐藏窗体
}
});
//设置窗体属性
this.setTitle("系统托盘测试");
this.setBounds(200,200,150,100);
this.setVisible(true);
}
//重写actionPerformed方法
public void actionPerformed(ActionEvent e){
if(e.getSource()==itemArray[0])
{//点击信息消息菜单项执行的动作
trayIcon.displayMessage("信息","程序最小化,仍在运行",TrayIcon.MessageType.INFO);
}else if(e.getSource()==itemArray[1])
{//点击信息消息菜单项执行的动作
trayIcon.displayMessage("常规信息","现在一切正常",TrayIcon.MessageType.NONE);
}else if(e.getSource()==itemArray[2])
{//单击警告消息菜单项执行的动作
trayIcon.displayMessage("警告信息","有不明来源的攻击",TrayIcon.MessageType.WARNING);
}else if(e.getSource()==itemArray[3])
{//点击错误消息菜单项执行的动作
trayIcon.displayMessage("错误信息","程序发生严重错误",TrayIcon.MessageType.ERROR);
}else if(e.getSource()==itemArray[4])
{//点击退出程序菜单项执行的动作
System.exit(0);
}else if(e.getSource()==trayIcon)
{//双击托盘图标执行的代码
this.show(true);
}
}
//主方法
public static void main(String args[])
{//创建SystemTrayTest窗体对象
new SystemTrayTest();
}
}
甚至还可以设定最小化到托盘的图标,这样的话,当有新消息到达时,你可以把它的图标设置成无色的另一图片(或者只将其最小化,而不设置其图标),然后再设置成原来的图片,如此反复,就实现了闪烁的功能.........
不过似乎先要判断当前操作系统是否支持系统托盘..........下面这个程序实现了系统托盘,但是似乎在我的电脑上没实现出来,难道操作系统不支持?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SystemTrayTest extends JFrame implements ActionListener
{
//创建菜单、菜单项数组、消息类型
PopupMenu popup=new PopupMenu();
Menu menu=new Menu("消息类型");
MenuItem[] itemArray ={new MenuItem("信息消息"),new MenuItem("常规消息"),
new MenuItem("警告消息"),new MenuItem("错误消息"),new MenuItem("退出程序")};
//定义系统托盘、托盘图标变量
SystemTray tray;
TrayIcon trayIcon;
//构造方法
public SystemTrayTest(){
//对菜单项添加监听并将菜单项添加到菜单中
for(int i=0;i<itemArray.length;i++){
if(i<4){
itemArray[i].addActionListener(this);//为菜单项注册监听器
//将菜单项数组中前3个菜单项添加进"弹出消息"菜单中
menu.add(itemArray[i]);
}
itemArray[4].addActionListener(this);//添加监听
popup.add(menu);//将弹出消息菜单添加到菜单中
popup.add(itemArray[4]);//将退出菜单添加到菜单中
}
// 判断当前操作系统是否支持系统托盘
if (SystemTray.isSupported()) {
//获取系统托盘
tray = SystemTray.getSystemTray();
//加载图标
Image image = Toolkit.getDefaultToolkit().getImage("d:/trayIcon.jpg");
//创建托盘图标
trayIcon=new TrayIcon(image,"系统托盘测试",popup);
//托盘图标自动设置尺寸
trayIcon.setImageAutoSize(true);
try{//添加托盘图标到系统托盘中
tray.add(trayIcon);
}
catch(AWTException e){
e.printStackTrace();
}
//为托盘图标注册监听器
trayIcon.addActionListener(this);
}
//设置窗体关闭按扭所执行的动作
this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
SystemTrayTest.this.hide();//隐藏窗体
}
});
//设置窗体属性
this.setTitle("系统托盘测试");
this.setBounds(200,200,150,100);
this.setVisible(true);
}
//重写actionPerformed方法
public void actionPerformed(ActionEvent e){
if(e.getSource()==itemArray[0])
{//点击信息消息菜单项执行的动作
trayIcon.displayMessage("信息","程序最小化,仍在运行",TrayIcon.MessageType.INFO);
}else if(e.getSource()==itemArray[1])
{//点击信息消息菜单项执行的动作
trayIcon.displayMessage("常规信息","现在一切正常",TrayIcon.MessageType.NONE);
}else if(e.getSource()==itemArray[2])
{//单击警告消息菜单项执行的动作
trayIcon.displayMessage("警告信息","有不明来源的攻击",TrayIcon.MessageType.WARNING);
}else if(e.getSource()==itemArray[3])
{//点击错误消息菜单项执行的动作
trayIcon.displayMessage("错误信息","程序发生严重错误",TrayIcon.MessageType.ERROR);
}else if(e.getSource()==itemArray[4])
{//点击退出程序菜单项执行的动作
System.exit(0);
}else if(e.getSource()==trayIcon)
{//双击托盘图标执行的代码
this.show(true);
}
}
//主方法
public static void main(String args[])
{//创建SystemTrayTest窗体对象
new SystemTrayTest();
}
}
展开全部
不知道java swing有没有提供这样的功能,估计没有。
qq的那个效果我想最终是通过调用操作系统api来解决的吧,所以你想要这样的效果必须求助于操作系统。
当然纯粹的java可能是无法直接调用windows api的,先要使用VC实现这一效果,然后将其编译成dll文件,再使用jni机制来调用这个dll,所以还是比较麻烦的,祝你好运吧。
qq的那个效果我想最终是通过调用操作系统api来解决的吧,所以你想要这样的效果必须求助于操作系统。
当然纯粹的java可能是无法直接调用windows api的,先要使用VC实现这一效果,然后将其编译成dll文件,再使用jni机制来调用这个dll,所以还是比较麻烦的,祝你好运吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*修订以上老答案!!!!!!!!!!!!!
JDK1.6及以后已经支持trayIcon
测试环境Eclipse,JDK1.8,package名russian
验证通过。*/
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle or the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package russian;
/*
* TrayIconDemo.java
*/
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
public class TrayIconDemo {
public static void main(String[] args) {
/* Use an appropriate Look and Feel */
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for the event-dispatching thread:
//adding TrayIcon.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
//Check the SystemTray support
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
final PopupMenu popup = new PopupMenu();
final TrayIcon trayIcon =
new TrayIcon(createImage("images/4leaves.png", "tray icon"));
final SystemTray tray = SystemTray.getSystemTray();
// Create a popup menu components
MenuItem aboutItem = new MenuItem("About");
CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
Menu displayMenu = new Menu("Display");
MenuItem errorItem = new MenuItem("Error");
MenuItem warningItem = new MenuItem("Warning");
MenuItem infoItem = new MenuItem("Info");
MenuItem noneItem = new MenuItem("None");
MenuItem exitItem = new MenuItem("Exit");
//Add components to popup menu
popup.add(aboutItem);
popup.addSeparator();
popup.add(cb1);
popup.add(cb2);
popup.addSeparator();
popup.add(displayMenu);
displayMenu.add(errorItem);
displayMenu.add(warningItem);
displayMenu.add(infoItem);
displayMenu.add(noneItem);
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("TrayIcon could not be added.");
return;
}
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"This dialog box is run from System Tray");
}
});
aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"This dialog box is run from the About menu item");
}
});
cb1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int cb1Id = e.getStateChange();
if (cb1Id == ItemEvent.SELECTED){
trayIcon.setImageAutoSize(true);
} else {
trayIcon.setImageAutoSize(false);
}
}
});
cb2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
int cb2Id = e.getStateChange();
if (cb2Id == ItemEvent.SELECTED){
trayIcon.setToolTip("Sun TrayIcon");
} else {
trayIcon.setToolTip(null);
}
}
});
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
MenuItem item = (MenuItem)e.getSource();
//TrayIcon.MessageType type = null;
System.out.println(item.getLabel());
if ("Error".equals(item.getLabel())) {
//type = TrayIcon.MessageType.ERROR;
trayIcon.displayMessage("Sun TrayIcon Demo",
"This is an error message", TrayIcon.MessageType.ERROR);
} else if ("Warning".equals(item.getLabel())) {
//type = TrayIcon.MessageType.WARNING;
trayIcon.displayMessage("Sun TrayIcon Demo",
"This is a warning message", TrayIcon.MessageType.WARNING);
} else if ("Info".equals(item.getLabel())) {
//type = TrayIcon.MessageType.INFO;
trayIcon.displayMessage("Sun TrayIcon Demo",
"This is an info message", TrayIcon.MessageType.INFO);
} else if ("None".equals(item.getLabel())) {
//type = TrayIcon.MessageType.NONE;
trayIcon.displayMessage("Sun TrayIcon Demo",
"This is an ordinary message", TrayIcon.MessageType.NONE);
}
}
};
errorItem.addActionListener(listener);
warningItem.addActionListener(listener);
infoItem.addActionListener(listener);
noneItem.addActionListener(listener);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon);
System.exit(0);
}
});
}
//Obtain the image URL
protected static Image createImage(String path, String description) {
URL imageURL = TrayIconDemo.class.getResource(path);
if (imageURL == null) {
System.err.println("Resource not found: " + path);
return null;
} else {
return (new ImageIcon(imageURL, description)).getImage();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询