开发语言:Java
实例大小:6.47M
实例类别:Java语言基础
【实例简介】
java swing ,用于包间计时,可暂停,sqlite数据库。
【实例截图】
【核心代码】
countdown
├── db
│ └── database.db
├── lib
│ └── sqlite-jdbc-3.21.0.1.jar
└── src
├── countdown
│ ├── Domain.java
│ ├── MainFrame.java
│ ├── MyTray.java
│ ├── SqliteHelp.java
│ └── ToolTime.java
└── image
├── about.png
├── biao.png
├── biao2.png
├── init.png
└── mytray.png
5 directories, 12 files
核心代码:
package countdown;
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.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class MyTray implements ActionListener, MouseListener {
private Image icon;// 图标
private TrayIcon trayIcon;
private SystemTray systemTray;// 系统托盘
private MainFrame myMainFrame; // 托盘所属主窗体
private PopupMenu pop = new PopupMenu(); // 弹出菜单
// 菜单选项
/** 还原 */
private MenuItem open = new MenuItem("open");
/** 退出*/
private MenuItem exit =new MenuItem("exit");
public MyTray(MainFrame myMainFrame) {
this.myMainFrame = myMainFrame;
// 得到托盘的图标
icon = new ImageIcon(this.getClass().getClassLoader().getResource(
"image/mytray.png")).getImage();
if (SystemTray.isSupported()) {
systemTray = SystemTray.getSystemTray();
// 设置鼠标经过图标时,显示的内容
trayIcon = new TrayIcon(icon, "显示窗口", pop);
pop.add(open);
//pop.add(exit);
// 添加系统托盘
try {
systemTray.add(trayIcon);
} catch (AWTException e1) {
e1.printStackTrace();
trayIcon.addMouseListener(this);
}
}
trayIcon.addMouseListener(this);
exit.addActionListener(this);
open.addActionListener(this);
}