当前位置:首页 » 编程语言 » java托盘

java托盘

发布时间: 2022-08-17 17:43:33

⑴ 我要用java做一个系统托盘,点最小化按钮,要怎么监听或者给我来个系统托盘的全代码 不要网上复制的

以下代码直接即可运行,注意修改一下托盘图标的文件路径!

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

⑵ java应用程序系统托盘的问题

演示程序,程序启动后会在托盘处显示一个黑色方块代表图标,鼠标移上去会有提示,右键点击会有菜单。

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JOptionPane;

public class TrayIconTest {
public static void main(String[] args) throws Exception {
TrayIcon ti = new TrayIcon(new BufferedImage(16, 16, 1));
ti.setImageAutoSize(true);
if (SystemTray.isSupported()) {
SystemTray.getSystemTray().add(ti);
ti.setToolTip("Trayicon test.");
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
if (ac.equalsIgnoreCase("action 1")) {
System.out.println("Menu 1 performed.");
} else if (ac.equalsIgnoreCase("action 2")) {
System.out.println("Menu 2 performed.");
JOptionPane.showMessageDialog(null, "Hello world.");
} else if (ac.equalsIgnoreCase("action 3")) {
System.out.println("Menu exit performed.");
System.exit(0);
}
}
};
PopupMenu pm = new PopupMenu();
MenuItem mi = new MenuItem("Menu 1");
mi.setActionCommand("action 1");
mi.addActionListener(l);
pm.add(mi);
mi = new MenuItem("Menu 2");
mi.setActionCommand("action 2");
mi.addActionListener(l);
pm.add(mi);
mi = new MenuItem("Menu exit");
mi.setActionCommand("action 3");
mi.addActionListener(l);
pm.add(mi);

ti.setPopupMenu(pm);
}
}
}

⑶ JAVA 系统托盘 图片不显示

可能是图没有了,setImageAutoSize(true)把这个设置下能不能看到图片的,再没有那就指定是图没拿到了。

⑷ java swing 系统托盘问题

java
最小化到系统托盘

2010-09-06 15:32
package test;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
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 javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MyTray {
TrayIcon trayIcon;//
托盘图标,但不是
Image
类型的



SystemTray Tray;//
系统托盘

Image
img=(new
ImageIcon("images/icon.png")).getImage();//
托盘图标,建
议使用较小的图片

public MyTray(){

final JFrame frame=new JFrame();
JPanel jp=new JPanel();
frame.add(jp);
frame.setSize(320,240);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setIconImage(img);//
设置窗口左上角的图标

frame.setVisible(true);
Tray=SystemTray.getSystemTray();//
获得系统托盘实例

//
创建系统托盘的右键弹出菜单

PopupMenu pm=new PopupMenu();
MenuItem mi0=new MenuItem("Open");
MenuItem mi1=new MenuItem("Close");
pm.add(mi0);
pm.add(mi1);

trayIcon=new TrayIcon(img,"",pm);//
创建托盘图标实例

trayIcon.setImageAutoSize(true);//
图标自动适应托盘,也就是说它自动
改变大小

trayIcon.setToolTip(" ");//
设置提示语

//
添加系统托盘图标

frame.addWindowListener(new
WindowAdapter()
{//
当“关闭”窗口时,最
小化到系统托盘

public void windowClosing(WindowEvent e) {
try {
Tray.add(trayIcon);
} catch (AWTException exc) {
exc.printStackTrace();
}
}});
mi0.addActionListener(new ActionListener(){ //
右键弹出菜单的事件
监听

public void actionPerformed(ActionEvent e) {
frame.setExtendedState(JFrame.NORMAL);
frame.setVisible(true);
Tray.remove(trayIcon);
}
});

mi1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
Tray.remove(trayIcon);//
退出程序,移出系统托盘处的图标

热点内容
android背单词源码 发布:2025-01-19 23:57:21 浏览:727
领动配置怎么样 发布:2025-01-19 23:56:35 浏览:83
python造数据 发布:2025-01-19 23:51:31 浏览:903
linux下卸载mysql 发布:2025-01-19 23:40:34 浏览:339
linuxweb路径 发布:2025-01-19 23:25:08 浏览:941
福建电信服务器ip地址 发布:2025-01-19 23:07:24 浏览:648
服务器怎么制作公告栏 发布:2025-01-19 23:06:23 浏览:874
英雄联盟皮肤源码 发布:2025-01-19 22:56:14 浏览:95
三星手机忘记解锁密码怎么办 发布:2025-01-19 22:45:43 浏览:292
Java为什么没有预编译命令 发布:2025-01-19 22:44:14 浏览:304