java swing工具提示与不同的消息
发布时间:2020-12-15 05:09:40 所属栏目:Java 来源:网络整理
导读:在 java swing中有没有办法用不同的消息显示工具提示,实际上我正在开发一个应用程序,我必须在系统任务栏中的工具提示上更新应用程序的当前状态. 提前致谢. 解决方法 1到Dan和Guillaume Polet.只需在trayIcon组件上使用setToolTipText()即可. 我为你做了一个
在
java swing中有没有办法用不同的消息显示工具提示,实际上我正在开发一个应用程序,我必须在系统任务栏中的工具提示上更新应用程序的当前状态.
提前致谢. 解决方法
1到Dan和Guillaume Polet.只需在trayIcon组件上使用setToolTipText()即可.
我为你做了一个简短的例子. 它将创建一个TrayIcon并将其添加到SystemTray.在TrayIcon的工具提示后,每5秒更新一次: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.MalformedURLException; import java.net.URL; import javax.swing.SwingUtilities; import javax.swing.Timer; public class SystemTrayExample { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new SystemTrayExample().createAndAddTrayIcon(); } }); } private void createAndAddTrayIcon() { try { initComponents(); } catch (MalformedURLException ex) { ex.printStackTrace(); } } private void initComponents() throws MalformedURLException { //Check the SystemTray is supported if (!SystemTray.isSupported()) { System.out.println("SystemTray is not supported"); return; } final PopupMenu popup = new PopupMenu(); final TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().createImage(new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/TrayIconDemoProject/src/misc/images/bulb.gif"))); trayIcon.setToolTip("I am the initial message"); final SystemTray tray = SystemTray.getSystemTray(); // Create a pop-up menu components MenuItem exitItem = new MenuItem("Exit"); exitItem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); //Add components to pop-up menu popup.add(exitItem); //set popmenu trayIcon.setPopupMenu(popup); try { tray.add(trayIcon); } catch (AWTException e) { System.out.println("TrayIcon could not be added."); } int delay = 5000; //milliseconds final Timer timer = new Timer(delay,new ActionListener() { int count = 1; @Override public void actionPerformed(ActionEvent evt) { System.out.println("Updating on EDT " + (SwingUtilities.isEventDispatchThread() ? "Yes" : "No")); if (count == 3) { trayIcon.setToolTip("I am the last message"); ((Timer) evt.getSource()).stop();//stop timer } if (count == 2) {//check if we should change tooltip trayIcon.setToolTip("I am the second message"); } if (count == 1) { trayIcon.setToolTip("I am the first message"); } count++; } }); timer.start();//start timer to change tooltip } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |