import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.TrayIcon;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.*;
import java.awt.event.*;public class MyFrame extends JFrame {
private TrayIcon trayIcon; public MyFrame() throws AWTException {
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (SystemTray.isSupported()) {
System.out.println("1:" );
setVisible(false);
try {
minimizeToTray();
} catch (AWTException ex) {
ex.printStackTrace();
}
} else {
System.exit(0);
}
}
});
JPanel root = new JPanel();
JButton exitButton = new JButton("ok");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
root.add(exitButton);
getContentPane().add(root);
pack();
initTrayIcon();
minimizeToTray(); } public void minimizeToTray() throws AWTException {
SystemTray tray = SystemTray.getSystemTray();
tray.add(trayIcon);
} private void initTrayIcon() {
Image image = null;
try {
image = Toolkit.getDefaultToolkit().getImage(this.getClass().
getResource("/aaa.gif"));
} catch (Exception ex) {
System.out.println("aaa:"+ex.getMessage()) ;
}
System.out.println("MyFrame:" + image);
PopupMenu popup = new PopupMenu();
MenuItem exitItem = new MenuItem("Show");
ActionListener showListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(true);
SystemTray.getSystemTray().remove(trayIcon);
}
};
exitItem.addActionListener(showListener);
popup.add(exitItem);
trayIcon = new TrayIcon(image, "MyTray", popup);
trayIcon.addActionListener(showListener);
} public static void main(String[] args) throws AWTException {
MyFrame frame = new MyFrame();
frame.setTitle("MyFrame");
frame.setVisible(true);
}
}在系统托盘那只显示空白,右键点击能出菜单,请高手指点!谢谢
import javax.swing.*;
import java.awt.TrayIcon;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.*;
import java.awt.event.*;public class MyFrame extends JFrame {
private TrayIcon trayIcon; public MyFrame() throws AWTException {
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
if (SystemTray.isSupported()) {
System.out.println("1:" );
setVisible(false);
try {
minimizeToTray();
} catch (AWTException ex) {
ex.printStackTrace();
}
} else {
System.exit(0);
}
}
});
JPanel root = new JPanel();
JButton exitButton = new JButton("ok");
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
root.add(exitButton);
getContentPane().add(root);
pack();
initTrayIcon();
minimizeToTray(); } public void minimizeToTray() throws AWTException {
SystemTray tray = SystemTray.getSystemTray();
tray.add(trayIcon);
} private void initTrayIcon() {
Image image = null;
try {
image = Toolkit.getDefaultToolkit().getImage(this.getClass().
getResource("/aaa.gif"));
} catch (Exception ex) {
System.out.println("aaa:"+ex.getMessage()) ;
}
System.out.println("MyFrame:" + image);
PopupMenu popup = new PopupMenu();
MenuItem exitItem = new MenuItem("Show");
ActionListener showListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(true);
SystemTray.getSystemTray().remove(trayIcon);
}
};
exitItem.addActionListener(showListener);
popup.add(exitItem);
trayIcon = new TrayIcon(image, "MyTray", popup);
trayIcon.addActionListener(showListener);
} public static void main(String[] args) throws AWTException {
MyFrame frame = new MyFrame();
frame.setTitle("MyFrame");
frame.setVisible(true);
}
}在系统托盘那只显示空白,右键点击能出菜单,请高手指点!谢谢
this.trayIcon = new TrayIcon(trayImage, "Java抓屏", this.getSytemTrayMenu());
image = new ImageIcon("images/leaf.gif").getImage();
应该是路径问题。