gpt4 book ai didi

java - PopupMenu 内的 JPanel

转载 作者:行者123 更新时间:2023-12-01 14:58:56 27 4
gpt4 key购买 nike

有没有办法在 PopupMenu 中渲染像 JPanel 一样的图形(使用 TrayIcon)?我知道使用 JPopupMenu 是可能的,但我不喜欢如果我单击弹出窗口外部,弹出窗口不会关闭(并且图标不会像 PopupMenu 那样突出显示)。

我尝试使用 JPopupMenu 执行的操作示例:

    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." );
}
}

最佳答案

Is there a way to render graphics like JPanel in a PopupMenu? I know it's possible by using JPopupMenu but I do not like that the popup doesn't close if I click outside of it (and the icon does not get highlighted as with PopupMenu).

  • 我只会直接谈论 Java-2D 到弹出容器,确保将 JPanel 与自定义绘画和 JButtons 一起放置没有问题,由 GridLayout

  • 放置
  • 是的,有几种方法,the best describtion around by @Kirill Grouchnikov

  • 您可以决定是否创建

    1) 为每个 JPopupMenu/JMenu 绘制一个新的油漆,

    2) 放入UIManager(然后对当前JVM中的所有对象有效)

关于java - PopupMenu 内的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13970322/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com