當前位置:首頁 » 編程語言 » java托盤

java托盤

發布時間: 2022-08-17 17:43:33

⑴ 我要用java做一個系統托盤,點最小化按鈕,要怎麼監聽或者給我來個系統托盤的全代碼 不要網上復制的

以下代碼直接即可運行,注意修改一下托盤圖標的文件路徑!

/////////////////////////////////////////////////////////////////////
import java.awt.AWTException;
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.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

public class SystemTrayTest extends JFrame {

public SystemTrayTest() throws AWTException {
this.setSize(500, 300);
this.setLocation(100, 50);

//判斷當前操作系統是否支持托盤
if (SystemTray.isSupported()) {
//獲取系統托盤
SystemTray tray = SystemTray.getSystemTray();

//托盤的右鍵菜單
PopupMenu menu = new PopupMenu();
//退出菜單
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

//顯示主窗口菜單
MenuItem showItem = new MenuItem("Show the main frame");
showItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
SystemTrayTest.this.setVisible(true);
}
});

//關於菜單(未注冊事件)
MenuItem aboutItem = new MenuItem("about...");

menu.add(exitItem);
menu.add(showItem);
menu.add(aboutItem);

//托盤的標題
String title = "Hey,Java!";
//托盤圖標
Image img = Toolkit.getDefaultToolkit().getImage("icon.jpg");
//實例化TrayIcon對象
TrayIcon icon = new TrayIcon(img, title, menu);
//托盤添加滑鼠事件
icon.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
SystemTrayTest.this.setVisible(true);
}
});

tray.add(icon);
icon.displayMessage("Hello!", "this is my DIY SystemTray!",
MessageType.INFO);// 運行程序的時候右下角會提示信息
}
}

public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new SystemTrayTest().setVisible(true);
} catch (AWTException e) {
e.printStackTrace();
}
}
});
}
}

⑵ java應用程序系統托盤的問題

演示程序,程序啟動後會在托盤處顯示一個黑色方塊代表圖標,滑鼠移上去會有提示,右鍵點擊會有菜單。

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JOptionPane;

public class TrayIconTest {
public static void main(String[] args) throws Exception {
TrayIcon ti = new TrayIcon(new BufferedImage(16, 16, 1));
ti.setImageAutoSize(true);
if (SystemTray.isSupported()) {
SystemTray.getSystemTray().add(ti);
ti.setToolTip("Trayicon test.");
ActionListener l = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
if (ac.equalsIgnoreCase("action 1")) {
System.out.println("Menu 1 performed.");
} else if (ac.equalsIgnoreCase("action 2")) {
System.out.println("Menu 2 performed.");
JOptionPane.showMessageDialog(null, "Hello world.");
} else if (ac.equalsIgnoreCase("action 3")) {
System.out.println("Menu exit performed.");
System.exit(0);
}
}
};
PopupMenu pm = new PopupMenu();
MenuItem mi = new MenuItem("Menu 1");
mi.setActionCommand("action 1");
mi.addActionListener(l);
pm.add(mi);
mi = new MenuItem("Menu 2");
mi.setActionCommand("action 2");
mi.addActionListener(l);
pm.add(mi);
mi = new MenuItem("Menu exit");
mi.setActionCommand("action 3");
mi.addActionListener(l);
pm.add(mi);

ti.setPopupMenu(pm);
}
}
}

⑶ JAVA 系統托盤 圖片不顯示

可能是圖沒有了,setImageAutoSize(true)把這個設置下能不能看到圖片的,再沒有那就指定是圖沒拿到了。

⑷ java swing 系統托盤問題

java
最小化到系統托盤

2010-09-06 15:32
package test;
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.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class MyTray {
TrayIcon trayIcon;//
托盤圖標,但不是
Image
類型的



SystemTray Tray;//
系統托盤

Image
img=(new
ImageIcon("images/icon.png")).getImage();//
托盤圖標,建
議使用較小的圖片

public MyTray(){

final JFrame frame=new JFrame();
JPanel jp=new JPanel();
frame.add(jp);
frame.setSize(320,240);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setIconImage(img);//
設置窗口左上角的圖標

frame.setVisible(true);
Tray=SystemTray.getSystemTray();//
獲得系統托盤實例

//
創建系統托盤的右鍵彈出菜單

PopupMenu pm=new PopupMenu();
MenuItem mi0=new MenuItem("Open");
MenuItem mi1=new MenuItem("Close");
pm.add(mi0);
pm.add(mi1);

trayIcon=new TrayIcon(img,"",pm);//
創建托盤圖標實例

trayIcon.setImageAutoSize(true);//
圖標自動適應托盤,也就是說它自動
改變大小

trayIcon.setToolTip(" ");//
設置提示語

//
添加系統托盤圖標

frame.addWindowListener(new
WindowAdapter()
{//
當「關閉」窗口時,最
小化到系統托盤

public void windowClosing(WindowEvent e) {
try {
Tray.add(trayIcon);
} catch (AWTException exc) {
exc.printStackTrace();
}
}});
mi0.addActionListener(new ActionListener(){ //
右鍵彈出菜單的事件
監聽

public void actionPerformed(ActionEvent e) {
frame.setExtendedState(JFrame.NORMAL);
frame.setVisible(true);
Tray.remove(trayIcon);
}
});

mi1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
Tray.remove(trayIcon);//
退出程序,移出系統托盤處的圖標

熱點內容
嫁個編程男 發布:2025-01-20 02:51:39 瀏覽:932
掛勞文件夾 發布:2025-01-20 02:44:22 瀏覽:520
寫編程英文 發布:2025-01-20 02:37:50 瀏覽:15
安卓怎麼修改飢荒 發布:2025-01-20 02:20:54 瀏覽:618
android64位開發環境 發布:2025-01-20 01:58:01 瀏覽:261
阿里雲伺服器能搭美國站點 發布:2025-01-20 01:49:34 瀏覽:276
安卓手機壁紙如何更換成動態壁紙 發布:2025-01-20 01:40:27 瀏覽:705
安卓微信簽名在哪裡修改 發布:2025-01-20 01:25:31 瀏覽:109
安卓電腦管家怎麼恢復出廠設置 發布:2025-01-20 01:24:06 瀏覽:313
qt編譯sqlite庫 發布:2025-01-20 01:22:30 瀏覽:525