?? java托盤.txt
字號:
在網上看了很多實現系統托盤圖標的帖子,但都大同小異:借用dll動態鏈接庫,用JAVA JNI實現。
我想的是有沒有用純JAVA實現的系統圖標?它應該不依賴于OS的,能在Windows下實現,在Linux下也可以運行實現的?!
對不起,只有這么點分了!有了可以再加。
回答:
up
回答:
jdk6.0中增加了java.awt.SystemTray,java.awt.SystemIcon支持系統托盤了。。
回答:
頂,曾經也考慮過這個問題!
回答:
使用 JDK 6 才可以。
回答:
public class DesktopTray {
private static Desktop desktop;
private static SystemTray st;
private static PopupMenu pm;
public static void main(String[] args) {
if(Desktop.isDesktopSupported()){//判斷目前平臺是否支援Desktop類
desktop = Desktop.getDesktop();
}
if(SystemTray.isSupported()){//判斷目前平臺是否支援系統托盤
st = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("netbeans.png");//定義托盤圖示的圖片
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(desktop!=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(desktop!=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(desktop!=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("http://blog.csdn.net/chinajash");
}
});
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);
}
}
回答:
很多Cpper也這么想,但是它們也做不到呢。
系統托盤,MAC就沒有這一說。怎么做才算是不依靠系統呢?
回答:
所謂“不依賴于平臺”,無非就是“為每個平臺都做一套,然后提供一個統一的調用接口”。
前面說的 JDK6 提供的功能,大概也就是這么做的。
回答:
JDK6.0的系統托盤的雛形其實是個開源的JDIC,其實現原理就是不同的系統調用不同的JNI
不是象LZ那樣想的,可以在任何平臺上跑,所以也是分支持不支持的,LZ可以用JDK1.6或JDIC
package csdn;
import java.awt.AWTException;
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.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
* Java1.6.0實現系統托盤技術演示
*
摘要:Java1.6.0實現系統托盤技術演示:本演示程序實現了點擊按鈕窗體關閉,
托盤圖標掛于系統托盤上,鼠標雙擊托盤圖標,窗體顯示,托盤圖標消失,在托盤圖標上點右鍵可彈出選擇菜單
(顯示窗口,退出系統,Author),分別能完成不同的任務要求。
* @author 五斗米 <如轉載請保留作者和出處 >
* @blog http://blog.csdn.net/mq612
*/
public class TrayDemo extends JFrame {
private JPanel pane = null;
private JButton button = null; // 啟動托盤圖標的按鈕
private JLabel label = null; // 用來顯示系統是否支持托盤的信息
private TrayIcon trayIcon = null; // 托盤圖標
private SystemTray tray = null; // 本操作系統托盤的實例
public TrayDemo() {
super("Java1.6.0托盤技術演示");
try {
// 將LookAndFeel設置成Windows樣式
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
pane = new JPanel();
button = new JButton("縮小到托盤");
button.setEnabled(false);
label = new JLabel("本操作系統不支持托盤");
pane.add(label);
pane.add(button);
if(SystemTray.isSupported()){ // 如果操作系統支持托盤
this.tray();
}
this.getContentPane().add(pane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setVisible(true);
}
/**
* 托盤相關代碼
*/
private void tray(){
label.setText("本操作系統支持托盤");
button.setEnabled(true);
tray = SystemTray.getSystemTray(); // 獲得本操作系統托盤的實例
ImageIcon icon = new ImageIcon("images/icon.gif"); // 將要顯示到托盤中的圖標
PopupMenu pop = new PopupMenu(); // 構造一個右鍵彈出式菜單
MenuItem show = new MenuItem("顯示窗口");
MenuItem exit = new MenuItem("退出演示");
MenuItem author = new MenuItem("Author");
/**
* TrayIcon有三個構造
* TrayIcon(Image image) 用“圖標”來構造
* TrayIcon(Image image, String tooltip) 用“圖標”和“ToolTip”構造
* TrayIcon(Image image, String tooltip, PopupMenu popup) 用“圖標”,“ToolTip”,“彈出菜單”來構造一個托盤圖標
*/
trayIcon = new TrayIcon(icon.getImage(), "Java1.6.0托盤技術演示", pop);
// 點擊本按鈕后窗口被關閉,托盤圖標被添加到系統的托盤中
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
tray.add(trayIcon); // 將托盤圖標添加到系統的托盤實例中
setVisible(false); // 使窗口不可視
} catch (AWTException ex) {
ex.printStackTrace();
}
}
});
/**
* 添加鼠標監聽器,當鼠標在托盤圖標上雙擊時,默認顯示窗口
*/
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2){ // 鼠標雙擊
tray.remove(trayIcon); // 從系統的托盤實例中移除托盤圖標
setVisible(true); // 顯示窗口
}
}
});
show.addActionListener(new ActionListener() { // 點擊“顯示窗口”菜單后將窗口顯示出來
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon); // 從系統的托盤實例中移除托盤圖標
setVisible(true); // 顯示窗口
}
});
exit.addActionListener(new ActionListener() { // 點擊“退出演示”菜單后退出程序
public void actionPerformed(ActionEvent e) {
System.exit(0); // 退出程序
}
});
author.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showMessage();
}
});
pop.add(show);
pop.add(exit);
pop.add(author);
}
/**
* 顯示信息
*/
private void showMessage(){
JOptionPane.showMessageDialog(this, new JLabel(" <html >作者:mq612(五斗米) <br >Blog:http://blog.csdn.net/mq612 </html >"), "五斗米", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
new TrayDemo();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -