JDK6.0发布有段时间了,新的JDK也有不少新的特性,我去网上搜集了一下,列在下面和大家一起学习.
1.Desktop和SystemTray. 在JDK6中 ,AWT新增加了两个类:Desktop和SystemTray,前者可以用来打开系统默认浏览器浏览指定的URL,打开系统默认邮件客户端给指定的邮箱发邮件,用默认应用程序打开或编辑文件(比如,用记事本打开以txt为后缀名的文件),用系统默认的打印机打印文档;后者可以用来在系统托盘区创建一个托盘程序。
我随便找了几张图,在Tray里面都是空的,没有图,可能是图太大,有xdjm知道希望告诉我
import
java.awt.AWTException;
import java.awt.Desktop;
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.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class DesktopTrayTest {
private static Desktop desktop;
private static SystemTray st;
private static PopupMenu pm;
public static void main( String[] args ) {
if ( Desktop.isDesktopSupported() ) {
desktop = Desktop.getDesktop();
}
if ( SystemTray.isSupported() ) {
st = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().createImage( " http://www.51ppt.com.cn/Article/Uploadphotos/200604/20064147333288.png " );
createPopupMenu();
TrayIcon ti = new TrayIcon( image, " Demo " , pm );
try {
st.add( ti );
} catch ( AWTException awte ) {
awte.printStackTrace();
}
}
}
public static void sendMail( String mail ) {
if ( desktop != null &&
desktop.isSupported( Desktop.Action.MAIL ) ) {
try {
desktop.mail( new URI( mail ) );
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
public static void openBrowser( String url ) {
if ( desktop != null &&
desktop.isSupported( Desktop.Action.BROWSE )) {
try {
desktop.browse( new URI( url ) );
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
public static void edit() {
if ( desktop != null &&
desktop.isSupported( Desktop.Action.EDIT ) ) {
File file = new File( " test.txt " );
try {
if ( file.exists() == false ) {
file.createNewFile();
}
desktop.edit( file );
} catch ( IOException ioe ) {
ioe.printStackTrace();
}
}
}
public static void createPopupMenu() {
pm = new PopupMenu();
MenuItem ob = new MenuItem( " Open url " );
ob.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
openBrowser( " http://blog.csdn.net/xumingming64398966 " );
}
} );
MenuItem sm = new MenuItem( " Send Mail " );
sm.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
sendMail( " 64398966@qq.com " );
}
} );
MenuItem ed = new MenuItem( " Edit " );
ed.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
edit();
}
} );
MenuItem ex = new MenuItem( " Exit " );
ex.addActionListener( new ActionListener()
import java.awt.Desktop;
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.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class DesktopTrayTest {
private static Desktop desktop;
private static SystemTray st;
private static PopupMenu pm;
public static void main( String[] args ) {
if ( Desktop.isDesktopSupported() ) {
desktop = Desktop.getDesktop();
}
if ( SystemTray.isSupported() ) {
st = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().createImage( " http://www.51ppt.com.cn/Article/Uploadphotos/200604/20064147333288.png " );
createPopupMenu();
TrayIcon ti = new TrayIcon( image, " Demo " , pm );
try {
st.add( ti );
} catch ( AWTException awte ) {
awte.printStackTrace();
}
}
}
public static void sendMail( String mail ) {
if ( desktop != null &&
desktop.isSupported( Desktop.Action.MAIL ) ) {
try {
desktop.mail( new URI( mail ) );
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
public static void openBrowser( String url ) {
if ( desktop != null &&
desktop.isSupported( Desktop.Action.BROWSE )) {
try {
desktop.browse( new URI( url ) );
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
public static void edit() {
if ( desktop != null &&
desktop.isSupported( Desktop.Action.EDIT ) ) {
File file = new File( " test.txt " );
try {
if ( file.exists() == false ) {
file.createNewFile();
}
desktop.edit( file );
} catch ( IOException ioe ) {
ioe.printStackTrace();
}
}
}
public static void createPopupMenu() {
pm = new PopupMenu();
MenuItem ob = new MenuItem( " Open url " );
ob.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
openBrowser( " http://blog.csdn.net/xumingming64398966 " );
}
} );
MenuItem sm = new MenuItem( " Send Mail " );
sm.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
sendMail( " 64398966@qq.com " );
}
} );
MenuItem ed = new MenuItem( " Edit " );
ed.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
edit();
}
} );
MenuItem ex = new MenuItem( " Exit " );
ex.addActionListener( new ActionListener()