本文共 1005 字,大约阅读时间需要 3 分钟。
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这里有个例子,你换成自己的图片试试就明白了
public class TestTray extends JFrame {
TrayIcon trayIcon = null;
public TestTray() {
try {
trayIcon = new TrayIcon(ImageIO.read(getClass().getResource("snow.png")));
} catch (IOException e2) {
e2.printStackTrace();
}
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
SystemTray.getSystemTray().remove(trayIcon);
setVisible(true);
int state = getExtendedState();
if ((state & JFrame.ICONIFIED) == JFrame.ICONIFIED) {
state = state - JFrame.ICONIFIED;
setExtendedState(state);
}
}
});
addWindowListener(new WindowAdapter() {
@Override
public void windowIconified(WindowEvent e) {
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e1) {
e1.printStackTrace();
}
setVisible(false);
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new TestTray();
}
}
转载地址:http://tlodl.baihongyu.com/