gpt4 book ai didi

java - 如何防止 TrayIcon 弹出窗口占用整个调度程序线程

转载 作者:行者123 更新时间:2023-11-30 07:17:02 26 4
gpt4 key购买 nike

我有一个使用 JFrame 以及 TrayIcon 的 java 应用程序,并且我向 TrayIcon< 添加了一个 PopupMenu/.

当我单击 TrayIcon 时,弹出菜单会显示,但只要 PopupMenu 可见,主框架就会卡住。

我的第一个想法是事件派发线程被某人占用。所以我编写了一个使用 Swing Worker 和进度条的小示例应用程序。

public class TrayIconTest {

public static void main(String[] args) throws AWTException {
JFrame frame = new JFrame("TrayIconTest");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

Container contentPane = frame.getContentPane();
JProgressBar jProgressBar = new JProgressBar();
BoundedRangeModel boundedRangeModel = jProgressBar.getModel();
contentPane.add(jProgressBar);

boundedRangeModel.setMinimum(0);
boundedRangeModel.setMaximum(100);

PopupMenu popup = new PopupMenu();
TrayIcon trayIcon =
new TrayIcon(new BufferedImage(64, 64, BufferedImage.TYPE_INT_RGB), "TEST");


// Create a popup menu components
MenuItem aboutItem = new MenuItem("About");
popup.add(aboutItem);
trayIcon.setPopupMenu(popup);
SystemTray tray = SystemTray.getSystemTray();
tray.add(trayIcon);

IndeterminateSwingWorker indeterminateSwingWorker = new IndeterminateSwingWorker(boundedRangeModel);
indeterminateSwingWorker.execute();


frame.setSize(640, 80);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}
}

class IndeterminateSwingWorker extends SwingWorker<Void, Integer> {

private BoundedRangeModel boundedRangeModel;

public IndeterminateSwingWorker(BoundedRangeModel boundedRangeModel) {
this.boundedRangeModel = boundedRangeModel;
}

@Override
protected Void doInBackground() throws Exception {
int i = 0;
long start = System.currentTimeMillis();
long runtime = TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES);
while (true) {
i++;
i %= boundedRangeModel.getMaximum();

publish(i);
Thread.sleep(20);

long now = System.currentTimeMillis();
if ((now - start) > runtime) {
break;
}

System.out.println("i = " + i);
}
return null;
}

@Override
protected void process(List<Integer> chunks) {
for (Integer chunk : chunks) {
boundedRangeModel.setValue(chunk);
}
}
}

如何重现

当您启动示例应用程序时,您将看到一个带有进度条的框架。 SwingWorker 模拟进度操作。

SwingWorker 发布进度值并将其打印到 System.out

当我单击托盘图标(“黑色”)时,进度条卡住并显示弹出菜单。我可以看到 SwingWorker 仍在后台运行,因为它将进度值输出到命令行。

调查

因此,我使用 jvisualvm 查看了该应用程序,它表明只要弹出窗口可见,事件调度程序线程就非常繁忙。

jvisualvm analysis

我可以发现,通过 sun.awt.windows.WTrayIconPeer.showPopupMenu(int, int) 方法可以使弹出窗口可见。

此方法使用 EventQueue.invokeLater 将可运行对象提交到事件调度线程。在此可运行程序中,调用方法 sun.awt.windows.WPopupMenuPeer._show。此方法是 native ,并且它似乎实现了一个繁忙的等待循环,以便事件调度线程完全被该方法占用。因此,只要弹出菜单可见,就不会执行其他 ui 相关任务。

有人知道在显示 TrayIcon 弹出窗口时保持事件调度线程响应的解决方法吗?

最佳答案

我现在使用 JPopupMenu 作为解决方法,但您无法将 JPopupMenu 直接添加到 TrayIcon

诀窍是编写一个MouseListener

public class JPopupMenuMouseAdapter extends MouseAdapter {

private JPopupMenu popupMenu;

public JPopupMenuMouseAdapter(JPopupMenu popupMenu) {
this.popupMenu = popupMenu;
}

@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}

@Override
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}

private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
Dimension size = popupMenu.getPreferredSize();
popupMenu.setLocation(e.getX() - size.width, e.getY() - size.height);
popupMenu.setInvoker(popupMenu);
popupMenu.setVisible(true);
}
}

并将其连接到TrayIcon

TrayIcon trayIcon = ...;
JPopupMenu popupMenu = ...;

JPopupMenuMouseAdapter jPopupMenuMouseAdapter = new JPopupMenuMouseAdapter(popupMenu);
trayIcon.addMouseListener(jPopupMenuMouseAdapter);

关于java - 如何防止 TrayIcon 弹出窗口占用整个调度程序线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38200403/

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