简体   繁体   English

Java。 最小化到系统托盘

[英]Java. Minimize to system tray

I have an app that checks password and should minimize to system tray if password is correct. 我有一个可检查密码的应用程序,如果密码正确,则应最小化到系统托盘。 But I don't know how to set frame invisible or minimize it using ActionListener . 但是我不知道如何使用ActionListenerframe设置为不可见或最小化。

public class MainWindow extends JFrame {
    private JPanel panel1;
    private JTextField Password;
    private JButton readyBtn;
    private JLabel label1;
    private JLabel label2;
    int count = 0;

    public MainWindow() {
        readyBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String password = Password.getText();
                if (password.equals("12345")) {
                    //minimize to system tray
                }
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("MainWindow");
        frame.setContentPane(new MainWindow().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        //frame.setUndecorated(true);
        frame.pack();

        TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().getImage("res/lock.png"));
        trayIcon.setToolTip("Running...");
        trayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                frame.setAlwaysOnTop(true);
                frame.setVisible(true);
                JOptionPane.showMessageDialog(null, "Clicked");
            }
        });

        try {
            SystemTray.getSystemTray().add(trayIcon);
        }catch (Exception e){
            System.out.println(e);
        }
    }
}

您可以使用

frame.setExtendedState(JFrame.ICONIFIED);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM