Java从托盘恢复JFrame

时间:2018-03-21 10:03:30

标签: java swing

我有一个实现智能卡读卡器监听器的应用程序。

当我运行应用程序时,我将其发送到托盘。

当应用程序检测到卡片时,我希望它将jFrame恢复正常,以便用户可以选择屏幕上可用的选项。

问题是,当我尝试恢复窗口时,它会创建一个新窗口。 如何将jFrame恢复正常。

这段代码就是我在jFrame构造函数中的代码: 它创建托盘图标和选项关闭和打开。但我想让它自动打开。是否可以以编程方式单击“打开”选项?

    initComponents();
    setResizable(false);
    setLocationRelativeTo(null);
    setTitle("App Title");
    UFRInstance = (uFrFunctions) Native.loadLibrary(GetLibFullPath(false), uFrFunctions.class);
    this.wsc = new WebSocketConnection();

    System.out.println("creating instance");
    try {
        System.out.println("setting look and feel");
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        System.out.println("Unable to set LookAndFeel");
    }
    if (SystemTray.isSupported()) {
        System.out.println("system tray supported");
        tray = SystemTray.getSystemTray();

        Image image = Toolkit.getDefaultToolkit().getImage("bulb.png");
        ActionListener exitListener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Exiting....");
                System.exit(0);
            }
        };
        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem("Close");
        defaultItem.addActionListener(exitListener);
        popup.add(defaultItem);
        defaultItem = new MenuItem("Open");
        defaultItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(true);
                setExtendedState(JFrame.NORMAL);
            }
        });
        popup.add(defaultItem);
        trayIcon = new TrayIcon(createIcon("bulb.png", "Icon"), "SystemTray Demo", popup);
        trayIcon.setImageAutoSize(true);
    } else {
        System.out.println("system tray not supported");
    }
    addWindowStateListener(new WindowStateListener() {
        public void windowStateChanged(WindowEvent e) {
            if (e.getNewState() == ICONIFIED) {
                try {
                    tray.add(trayIcon);
                    setVisible(false);
                    System.out.println("added to SystemTray");
                } catch (AWTException ex) {
                    System.out.println("unable to add to tray");
                }
            }
            if (e.getNewState() == 7) {
                try {
                    tray.add(trayIcon);
                    setVisible(false);
                    System.out.println("added to SystemTray");
                } catch (AWTException ex) {
                    System.out.println("unable to add to system tray");
                }
            }
            if (e.getNewState() == MAXIMIZED_BOTH) {
                tray.remove(trayIcon);
                setVisible(true);
                System.out.println("Tray icon removed");
            }
            if (e.getNewState() == NORMAL) {
                tray.remove(trayIcon);
                setVisible(true);
                System.out.println("Tray icon removed1");
            }
        }
    });
    setIconImage(Toolkit.getDefaultToolkit().getImage("bulb.png"));
    setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("bulb.png")));
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

2 个答案:

答案 0 :(得分:0)

您的代码包含" Open"选项,添加ActionListener:

    defaultItem = new MenuItem("Open");
    defaultItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        setVisible(true);
        setExtendedState(JFrame.NORMAL);
    }

所以你需要做的就是调用setVisible(true);,也许是第二行。

答案 1 :(得分:0)

你需要做的可见。如下例:

int state = getExtendedState();
state = state & ~Frame.ICONIFIED;
setExtendedState(state);
setVisible(true);

第一行检索当前状态,即位掩码。然后,它将ICONIFIED(最小化)部分设置为false,然后再次设置相同的状态。这样,如果程序在最大化之前再次最大化,等等。当然必须按照setVisible(true)再次显示窗口。