我要用JAVA做一个系统托盘,点最小化按钮,要怎么监听?或者给我来个系统托盘的全代码 不要网上复制的
1个回答
展开全部
以下代码直接copy即可运行,注意修改一下托盘图标的文件路径!
/////////////////////////////////////////////////////////////////////
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
public class SystemTrayTest extends JFrame {
public SystemTrayTest() throws AWTException {
this.setSize(500, 300);
this.setLocation(100, 50);
//判断当前操作系统是否支持托盘
if (SystemTray.isSupported()) {
//获取系统托盘
SystemTray tray = SystemTray.getSystemTray();
//托盘的右键菜单
PopupMenu menu = new PopupMenu();
//退出菜单
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//显示主窗口菜单
MenuItem showItem = new MenuItem("Show the main frame");
showItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
SystemTrayTest.this.setVisible(true);
}
});
//关于菜单(未注册事件)
MenuItem aboutItem = new MenuItem("about...");
menu.add(exitItem);
menu.add(showItem);
menu.add(aboutItem);
//托盘的标题
String title = "Hey,Java!";
//托盘图标
Image img = Toolkit.getDefaultToolkit().getImage("icon.jpg");
//实例化TrayIcon对象
TrayIcon icon = new TrayIcon(img, title, menu);
//托盘添加鼠标事件
icon.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
SystemTrayTest.this.setVisible(true);
}
});
tray.add(icon);
icon.displayMessage("Hello!", "this is my DIY SystemTray!",
MessageType.INFO);// 运行程序的时候右下角会提示信息
}
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new SystemTrayTest().setVisible(true);
} catch (AWTException e) {
e.printStackTrace();
}
}
});
}
}
/////////////////////////////////////////////////////////////////////
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
public class SystemTrayTest extends JFrame {
public SystemTrayTest() throws AWTException {
this.setSize(500, 300);
this.setLocation(100, 50);
//判断当前操作系统是否支持托盘
if (SystemTray.isSupported()) {
//获取系统托盘
SystemTray tray = SystemTray.getSystemTray();
//托盘的右键菜单
PopupMenu menu = new PopupMenu();
//退出菜单
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//显示主窗口菜单
MenuItem showItem = new MenuItem("Show the main frame");
showItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
SystemTrayTest.this.setVisible(true);
}
});
//关于菜单(未注册事件)
MenuItem aboutItem = new MenuItem("about...");
menu.add(exitItem);
menu.add(showItem);
menu.add(aboutItem);
//托盘的标题
String title = "Hey,Java!";
//托盘图标
Image img = Toolkit.getDefaultToolkit().getImage("icon.jpg");
//实例化TrayIcon对象
TrayIcon icon = new TrayIcon(img, title, menu);
//托盘添加鼠标事件
icon.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
SystemTrayTest.this.setVisible(true);
}
});
tray.add(icon);
icon.displayMessage("Hello!", "this is my DIY SystemTray!",
MessageType.INFO);// 运行程序的时候右下角会提示信息
}
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new SystemTrayTest().setVisible(true);
} catch (AWTException e) {
e.printStackTrace();
}
}
});
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询