测试代码包括2个文件: TaryFrame.java 和 Tary.java.
/////////////////////////////////////////////// 1. TaryFrame.java //////////////////////////////////////////////////////
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TaryFrame extends JFrame{
public static JTextArea jta_info = new JTextArea(10,30);
public JTextField jtf_port;
public TaryFrame(){
this.setTitle("tray test");
this.setSize(410,380);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//窗体大小固定
setResizable(false);
new Tary(this);
this.setVisible(true);
}
public static void main(String[] args) {
new TaryFrame();
}
}
/////////////////////////////////////////////// 2. Tary.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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class Tary implements ActionListener,MouseListener,WindowListener{
TrayIcon trayIcon=null;
Image iconImage= Toolkit.getDefaultToolkit().getImage("img/trayicon.png");
JFrame frame;
public Tary(JFrame f){
frame=f;
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
PopupMenu popup = new PopupMenu();
//popup.addActionListener(this);
MenuItem defaultItem = new MenuItem("打开");
defaultItem.addActionListener(this);
popup.add(defaultItem);
defaultItem = new MenuItem("退出");
defaultItem.addActionListener(this);
popup.add(defaultItem);
trayIcon = new TrayIcon(iconImage, "Tray Test", popup);
trayIcon.addMouseListener(this);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
}
frame.setIconImage(iconImage);
frame.addWindowListener(this);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void windowActivated(WindowEvent arg0) {
//this.setAlwaysOnTop(true);
}
public void windowClosed(WindowEvent arg0) {
}
public void windowClosing(WindowEvent arg0) {
}
public void windowDeactivated(WindowEvent arg0) {
}
public void windowDeiconified(WindowEvent arg0) {
}
public void windowIconified(WindowEvent arg0) {
frame.setVisible(false);
}
public void windowOpened(WindowEvent arg0) {
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("退出"))
System.exit(0);
else if(e.getActionCommand().equals("打开"))
frame.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2)
{
frame.setVisible(true);
frame.setExtendedState(JFrame.NORMAL);
frame.repaint();
}
}
public void mouseEntered(MouseEvent arg0) {
System.out.println("mouseEntered ");
}
public void mouseExited(MouseEvent arg0) {
System.out.println("mouseExited ");
}
public void mousePressed(MouseEvent arg0) {
System.out.println("mousePressed ");
}
public void mouseReleased(MouseEvent arg0) {
System.out.println("mouseReleased ");
}
}
本文提供了一个使用Java实现系统托盘程序的示例代码,包括如何创建托盘图标、设置弹出菜单以及处理鼠标点击事件等关键步骤。
2万+

被折叠的 条评论
为什么被折叠?



