Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the Desktop and SystemTray Class characteristics of JDK6.0

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to analyze the Desktop and SystemTray features of JDK6.0. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

In JDK6, AWT added two new classes: Desktop and SystemTray, the former can be used to open the system default browser to browse the specified URL, open the system default mail client to send mail to the specified mailbox, open or edit files with the default application (for example, using notepad to open files with the suffix txt), and print documents with the system's default printer; the latter can be used to create a tray program in the system tray area. The following code demonstrates the use of Desktop and SystemTray.

/ * *

*

* @ author chinajash

, /

Public class DesktopTray {

Private static Desktop desktop

Private static SystemTray st

Private static PopupMenu pm

Public static void main (String [] args) {

If (Desktop.isDesktopSupported ()) {/ / determine whether the current platform supports Desktop classes

Desktop = Desktop.getDesktop ()

}

If (SystemTray.isSupported ()) {/ / determine whether the current platform supports system trays

St = SystemTray.getSystemTray ()

Image image = Toolkit.getDefaultToolkit () .getImage ("netbeans.png"); / / define the picture of the tray icon

CreatePopupMenu ()

TrayIcon ti = new TrayIcon (image, "Desktop Demo Tray", pm)

Try {

St.add (ti)

} catch (AWTException ex) {

Ex.printStackTrace ()

}

}

}

Public static void sendMail (String mail) {

If (desktopically empty & null & desktop.isSupported (Desktop.Action.MAIL)) {

Try {

Desktop.mail (new URI (mail))

} catch (IOException ex) {

Ex.printStackTrace ()

} catch (URISyntaxException ex) {

Ex.printStackTrace ()

}

}

}

Public static void openBrowser (String url) {

If (desktopically empty & null & desktop.isSupported (Desktop.Action.BROWSE)) {

Try {

Desktop.browse (new URI (url))

} catch (IOException ex) {

Ex.printStackTrace ()

} catch (URISyntaxException ex) {

Ex.printStackTrace ()

}

}

}

Public static void edit () {

If (desktopically empty & null & desktop.isSupported (Desktop.Action.EDIT)) {

Try {

File txtFile = new File ("test.txt")

If (! txtFile.exists ()) {

TxtFile.createNewFile ()

}

Desktop.edit (txtFile)

} catch (IOException ex) {

Ex.printStackTrace ()

}

}

}

Public static void createPopupMenu () {

Pm = new PopupMenu ()

MenuItem openBrowser = new MenuItem ("Open My Blog")

OpenBrowser.addActionListener (new ActionListener () {

Public void actionPerformed (ActionEvent e) {

OpenBrowser ("")

}

});

MenuItem sendMail = new MenuItem ("Send Mail to me")

SendMail.addActionListener (new ActionListener () {

Public void actionPerformed (ActionEvent e) {

SendMail ("mailto:chinajash@yahoo.com.cn")

}

});

MenuItem edit = new MenuItem ("Edit Text File")

SendMail.addActionListener (new ActionListener () {

Public void actionPerformed (ActionEvent e) {

Edit ()

}

});

MenuItem exitMenu = new MenuItem (& Exit)

ExitMenu.addActionListener (new ActionListener () {

Public void actionPerformed (ActionEvent e) {

System.exit (0)

}

});

Pm.add (openBrowser)

Pm.add (sendMail)

Pm.add (edit)

Pm.addSeparator ()

Pm.add (exitMenu)

}

} http://blog.csdn.net/chinajash

If you run the program in Windows, you can see that there is an icon in the system tray area. Right-clicking the icon will pop up a menu, click Open My Blog to open IE, and browse "http://blog.csdn.net/chinajash";, click Send Mail to me to open Outlook Express and send me an email." clicking Edit Text File will open notepad to edit the file created in the program test.txt

After reading the above, do you have any further understanding of how to analyze the Desktop and SystemTray class features of JDK6.0? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report