简体   繁体   中英

Java TrayIcon message not displaying

I am trying to get a basic system tray message to appear in Windows 8.1 using TrayIcon. However, nothing is showing up when I run the program. This is the code:

package alert1;

import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.imageio.*;

public class Main {
    public static void main(String[] args) throws IOException {
        URL gfl = new URL("http://gflclan.com/GFL/serverlist.php");
        BufferedReader in = new BufferedReader(new InputStreamReader(gfl.openStream()));

        Image img = ImageIO.read(new File("gflicon.jpg"));
        TrayIcon tray = new TrayIcon(img);

        System.out.println("Enter name of map: ");
        Scanner scan = new Scanner(System.in); //retrieves name of map from IO
        String str = scan.nextLine();
        scan.close();

                            //pL = previousLine
        String pL1 = null;  //line which contains the server name
        String pL2 = null;
        String pL3 = null;
        String pL4 = null;  //line which contains the server IP
        String pL5 = null;
        String currentLine;
        while ((currentLine = in.readLine()) != null)
            if(currentLine.contains(str)){
                String pL1fixed = pL1.replaceAll("\\<.*?\\> ?", "").trim(); //removes HTML/CSS formatting
                String pL4fixed = pL4.replaceAll("\\<.*?\\> ?", "").trim();
                System.out.println("Server Name: " + pL1fixed);
                System.out.println("Server IP: " + pL4fixed);
                tray.displayMessage("Server Found", "[Server Info Here]", TrayIcon.MessageType.WARNING);
            } else {
                pL1 = pL2; //updates stream's line history
                pL2 = pL3;
                pL3 = pL4;
                pL4 = pL5;
                pL5 = currentLine;
            }
        in.close();
    }
}

Is there something I am missing? As far as I can tell, I have the TrayIcon object and I have called displayMessage on it, so I do not know why it is not showing up. This is my first Java project and my first time working with images so forgive me if this code is very amateur.

First of all, take a look at How to Use the System Tray and the JavaDocs for SystemTray which have a number of examples

Basically, you're not adding your TrayIcon to anything

Abbreviated example taken from SystemTray JavaDocs

if (SystemTray.isSupported()) {
     SystemTray tray = SystemTray.getSystemTray();
     Image image = ...;
     trayIcon = new TrayIcon(image, "Tray Demo");
     try {
         tray.add(trayIcon);
     } catch (AWTException e) {
         System.err.println(e);
     }
}

Second, you really shouldn't mix console based programs with GUI's, they have different ways of working which are generally incompatible with each other

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