我试图获得一个基本的系统托盘信息,以显示在Windows8.1使用TrayIcon。然而,当我运行这个程序时,什么也没有出现。这是代码:
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();
}
}我遗漏了什么吗?据我所知,我有TrayIcon对象,并且在它上调用了displayMessage,所以我不知道为什么它没有出现。这是我的第一个Java项目,也是我第一次处理图像,所以如果这段代码非常业余,请原谅我。
发布于 2015-06-27 05:46:02
首先,看看如何使用系统托盘和SystemTray,它们有很多例子
基本上,你没有把你的TrayIcon添加到任何东西中
摘自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);
}
}第二,你真的不应该把基于控制台的程序和GUI的程序混在一起,它们有不同的工作方式,通常是互不兼容的。
https://stackoverflow.com/questions/31085556
复制相似问题