右键单击应用系统托盘时,Swing会冻结(显示)

时间:2015-07-23 08:14:44

标签: java swing

右键单击应用程序托盘图标(触发显示托盘弹出菜单)时,应用程序冻结。找到旧的答案(2008),这是因为java调用了一些本机方法来显示它。有没有办法解决/解决这个问题?

以下快速决策:

package test;

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class TestApp {
    private static JFrame mainFrame;

public static void main(String[] args) {
    mainFrame = new JFrame();
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            start();
        }
    });
}

private static void start() {
    initApp();

    SystemTray systemTray;
    PopupMenu popupMenu = new PopupMenu();
    TrayIcon trayIcon = new TrayIcon(new ImageIcon("C:\\test\\image.png").getImage(), "test", popupMenu);


    if (SystemTray.isSupported()) {
        systemTray = SystemTray.getSystemTray();
        initializePopupMenu(popupMenu);
        try {
            systemTray.add(trayIcon);
        } catch (AWTException e) {
            //                      throw new TrayNotSupportedException("Tray is not supported.");
        }
    }
}

private static void initializePopupMenu(PopupMenu popupMenu) {
    popupMenu.add(new MenuItem("test Item"));
}

private static void initApp() {
    JProgressBar progress = new JProgressBar();
    progress.setIndeterminate(true);
    mainFrame.getContentPane().add(progress);
    mainFrame.setSize(400, 400);
    mainFrame.setVisible(true);
}

}

0 个答案:

没有答案