システムトレイ用の小さなプログラムを作成しています。そこにいくつかの情報が表示され、テキストの横のポップアップメニューにアイコンが表示されます。 JMenuItemを使用してアイコンを追加しようとしましたが、PopupMenuを追加できません。私はmenuItemを使用しても、私はアイコンを追加することはできません。ここでPopupMenuのmenuItemの一部としてテキストの横にアイコン/イメージを追加します。
は、これまでの私のコードです:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//......
public class Interface
{
public static void mainInterface()
{
if(!SystemTray.isSupported())
{
System.out.println("System tray not supported! This program can only be used on Windows.");
return ;
}
SystemTray systemTray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("src/icon.png");
PopupMenu trayPopupMenu = new PopupMenu();
MenuItem close = new MenuItem("Close");
MenuItem CMItem = new MenuItem("test1", new ImageIcon("src/greenIcon.png"));
MenuItem HerItem = new MenuItem("test2", new ImageIcon("src/greenIcon.png"));
close.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
trayPopupMenu.add(close);
trayPopupMenu.add(CMItem);
trayPopupMenu.add(HerItem);
TrayIcon trayIcon = new TrayIcon(image, "Status", trayPopupMenu);
trayIcon.setImageAutoSize(true);
try
{
systemTray.add(trayIcon);
}catch(AWTException awtException)
{
awtException.printStackTrace();
}
System.out.println("end of main");
}
}
上記のコードはのMenuItemオブジェクトであるため、アイコン部分の実行されません。誰かがメニューのテキストの横にアイコンを置いて助けてくれますか?
編集:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor MenuItem(String, ImageIcon) is undefined
The constructor MenuItem(String, ImageIcon) is undefined
at minecraft.status.checker.Interface.mainInterface(Interface.java:23)
at minecraft.status.checker.MainClass.main(MainClass.java:12)
質問を編集して、取得しているスタックトレースを追加できますか? –
@AndrewG追加しました – MicrosoftDave