Java TrayIcon 消息未显示
Java TrayIcon message not displaying
我正在尝试使用 TrayIcon 使基本系统托盘消息显示在 Windows 8.1 中。但是,当我 运行 程序时,什么也没有出现。这是代码:
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 项目,也是我第一次使用图像,如果此代码非常业余,请原谅我。
首先看一下How to Use the System Tray and the JavaDocs for 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 混合使用,它们具有不同的工作方式,通常彼此不兼容
我正在尝试使用 TrayIcon 使基本系统托盘消息显示在 Windows 8.1 中。但是,当我 运行 程序时,什么也没有出现。这是代码:
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 项目,也是我第一次使用图像,如果此代码非常业余,请原谅我。
首先看一下How to Use the System Tray and the JavaDocs for 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 混合使用,它们具有不同的工作方式,通常彼此不兼容