简体   繁体   中英

trayicon.displaymessage text size java

This is my first post in this forum and I really hope that it will be answered ASAP. I'm new to Java and loves trying different things often. I thought of making simple balloon message application in Java that pops up at certain time in the system tray conveying different message over time. Just wondering if I could change the text size of the balloon message. Also if somebody can help me out with time intervals and delays for the message to appear and disappear. Below is my code that I tried, please ponder a bit to help me out with this problem.

import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import javax.swing.ImageIcon;


 public class BubbleMessages {


 public static void main(String[] args) throws Exception{
    SystemTray tray = SystemTray.getSystemTray();
  Image i = new ImageIcon("resources/bulb.gif").getImage();
  TrayIcon ti = new TrayIcon(i);
  tray.add(ti);
  ti.displayMessage("Message", "message", TrayIcon.MessageType.INFO);

  }
  }

Check the api . The message will automatically disappear with a user click.

If you want to display the message again, you might use Timer

You can modify the code like this

import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import javax.swing.ImageIcon;
public class BubbleMessages {
    private static TrayIcon ti;
    public static void main(String[] args) throws Exception{
        SystemTray tray = SystemTray.getSystemTray();
        Image i = new ImageIcon("resources/bulb.gif").getImage();
        ti = new TrayIcon(i);
        tray.add(ti);
        MessageDisplayTask mdt = new MessageDisplayTask(ti);
        java.util.Timer timer = new java.util.Timer("DM");
        timer.schedule(mdt, 0, 10000);//Every three seconds, it shows a message
    }
}
class MessageDisplayTask extends java.util.TimerTask {
    private TrayIcon ti;
    private int displayCount = 0;
    public MessageDisplayTask(TrayIcon ti){
        this.ti = ti;
    }
    public void run() {
        displayCount++;
        if (displayCount <= 10) {
            ti.displayMessage("Message", "Message#" + displayCount, TrayIcon.MessageType.INFO);
        } else {
            //Stop Timer.
            this.cancel();
        }
    }
}
tray.add(ti);
ti.setImageAutoSize(true);
ti.displayMessage("Message", "message", TrayIcon.MessageType.INFO);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM