我有一个 Java 应用程序,它通过右键菜单在系统托盘中放置一个图标。当我从 IDE (NetBeans) 运行它时,它可以正常工作。但是当我构建它并运行 JAR 文件时,它会将图标放在系统托盘中,但是当我右键单击它时,它没有上下文菜单。
这是一些代码。任何帮助表示赞赏。我检查了 Java 的平台版本和控制台版本,它们看起来都是一样的。
package abaxis;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Systray
{
private TrayIcon trayIcon = null;
private Image databaseimage;
private Image databaseDisconnectedImage;
public PopupMenu popup;
private ActionListener exitListener;
private ActionListener settingsListener;
private ActionListener connectListener;
private ActionListener disconnectListener;
private static Systray instance;
private static Abaxis abaxis = null;
private SystemTray tray;
private MenuItem connectItem;
private MenuItem settingsItem;
private MenuItem exitItem;
private MenuItem disconnectItem;
public Systray()
{
// create a action listener to listen for default action executed on the tray icon
exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
};
settingsListener = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//New Jframe.
JFrame settingsJFrame = new AbaxisSettingsForm();
settingsJFrame.setVisible(true);
}
};
connectListener = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Start the application.
abaxis.connect();
setConnectedItems();
}
};
disconnectListener = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Discconect the application.
abaxis.disconnect();
setDisconnectedItems();
}
};
}
public static Systray getSystray()
{
if(instance == null)
{
instance = new Systray();
instance.init();
}
return instance;
}
public void init()
{
// get the SystemTray instance
tray = SystemTray.getSystemTray();
// load Images
databaseimage = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/database.png"));
databaseDisconnectedImage = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/database_delete.png"));
// create a popup menu
this.popup = new PopupMenu();
// create menu item for the default action
connectItem = new MenuItem("Connect");
connectItem.addActionListener(connectListener);
settingsItem = new MenuItem("Settings");
settingsItem.addActionListener(settingsListener);
exitItem = new MenuItem("Exit");
exitItem.addActionListener(exitListener);
disconnectItem = new MenuItem("Disconnect");
disconnectItem.addActionListener(disconnectListener);
// set the TrayIcon properties
//trayIcon.addActionListener(exitListener);
// construct a TrayIcon
trayIcon = new TrayIcon( databaseimage, "Abaxis", this.popup );
// add the tray image
try
{
tray.add(trayIcon);
}
catch (AWTException e)
{
System.err.println(e);
}
try
{
abaxis = Abaxis.start();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
if(abaxis.isConnected())
{
//Set all the stuff if its connected.
setConnectedItems();
}
else
{
//Set all the stuff if its disconnected.
setDisconnectedItems();
}
}
private void setConnectedItems()
{
this.popup.removeAll();
this.popup.add(disconnectItem);
this.popup.add(settingsItem);
this.popup.add(exitItem);
this.updateImage(databaseimage);
}
private void setDisconnectedItems()
{
this.popup.removeAll();
this.popup.add(connectItem);
this.popup.add(settingsItem);
this.popup.add(exitItem);
this.updateImage(databaseDisconnectedImage);
}
public void updateImage(Image x)
{
if (this.trayIcon != null)
{
this.trayIcon.setImage(x);
}
}
}
谢谢我设法发现这是一个缺少罐子的问题。但是 jar 文件似乎在 lib 文件夹中。所以我不知道为什么它有一个翼。堆栈跟踪:
java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:123)
at abaxis.Abaxis.connectToBloodMachine(Abaxis.java:114)
at abaxis.Abaxis.connect(Abaxis.java:90)
at abaxis.Abaxis.init(Abaxis.java:76)
at abaxis.Abaxis.start(Abaxis.java:53)
at abaxis.Systray.init(Systray.java:141)
at abaxis.Systray.getSystray(Systray.java:88)
at abaxis.Abaxis.main(Abaxis.java:279)