Java创建系统托盘实现(swt和jdk6分别实现)Java认证考试
文章作者 100test 发表时间 2010:02:11 02:23:12
来源 100Test.Com百考试题网
由于jdk6之前并不支持
所以使用swt
jdk6可以支持了 建议使用jdk6的版本 因为它才是真正的跨平台
swt
Java代码
import org.eclipse.swt.*.
import org.eclipse.swt.graphics.*.
import org.eclipse.swt.layout.FillLayout.
import org.eclipse.swt.widgets.*.
public class TrayBySwt {
public static void main(String[] args) {
Display display = new Display().
final Shell shell = new Shell(display).
shell.setText("系统托盘BySwt").
shell.setLayout (new FillLayout()).
Button button = new Button(shell, SWT.PUSH).
button.setText("隐藏窗口显示托盘图标").
ImageData imageData = new ImageData("E:\\workspace\\myeclipse66\\ra\\Tray\\src\\16.gif").
Image image = new Image(display, imageData).
//取得系统托盘
final Tray tray = display.getSystemTray().
if (tray == null) {
System.out.println("当前操作系统不支持托盘图标").
} else {
final TrayItem item = new TrayItem(tray, SWT.NONE).
item.setToolTipText("我是托盘图标").
item.addListener (SWT.Show, new Listener () {
public void handleEvent (Event event) {
System.out.println("show").
}
}).
item.addListener (SWT.Hide, new Listener () {
public void handleEvent (Event event) {
System.out.println("hide").
}
}).
item.addListener (SWT.Selection, new Listener () {
public void handleEvent (Event event) {
System.out.println("0selection").
}
}).