有沒有辦法在PopupMenu(使用TrayIcon)渲染像JPanel這樣的圖形?我知道這可以通過使用JPopupMenu,但我不喜歡彈出不會關閉,如果我點擊它之外(和圖標不突出像PopupMenu一樣)。什麼我試圖用的JPopupMenu做PoppaMenu內部的JPanel
例子:
if(SystemTray.isSupported()) {
// Get the SystemTray instance
SystemTray tray = SystemTray.getSystemTray();
// Load icon
Image image = new ImageIcon(this.getClass().getResource("delete.png")).getImage();
final JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("Test"));
JPanel p1 = new JPanel();
p1.setBackground(Color.red);
p1.setPreferredSize(new Dimension(200, 300));
popup.add(p1);
JTrayIcon trayIcon = new JTrayIcon(image);
trayIcon.setJPopupMenu(popup);
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
popup.setLocation(e.getX(), e.getY());
popup.setInvoker(popup);
popup.setVisible(true);
}
});
try {
tray.add(trayIcon);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Could not add tray icon.");
}
}
一個使用JMenuItem的,這是不使用JPopupMenu時出現問題。問題是我想用TrayIcon顯示一個帶有圖形的彈出窗口。 –
我只是從SystemTrays圖標談論JPopup的mousevents圖標 – mKorbel