How to hide a JFrame in system tray of taskbar

后端 未结 3 2057
天命终不由人
天命终不由人 2020-11-27 14:30

I\'d created a JFrame and want to hide in taskbar (in windows not visible right in the bottom but hidden in the tray menu items<

3条回答
  •  清酒与你
    2020-11-27 14:46

    myFrame#getExtendedState and myFrame#setExtendedState

    basically is better to multiplay these ExtendedStates

    frame.setExtendedState(JFrame.ICONIFIED);
    frame.setExtendedState(frame.getExtendedState() | JFrame.ICONIFIED);
    
    
    frame.setExtendedState(JFrame.NORMAL);
    frame.setExtendedState(frame.getExtendedState() & (~JFrame.ICONIFIED));
    

    EDIT 1.

    for example

    import java.awt.event.ActionEvent;
    import javax.swing.*;  
    public class WindowGCDemo1 {  
        private javax.swing.Timer timer = null;
        private int count = 0;
        private JFrame myFrame = new JFrame();  
        public WindowGCDemo1() {
            myFrame = new JFrame();
            myFrame.setLocation(150, 150);
            myFrame.setSize(200, 400);
            myFrame.setVisible(true);
            System.out.println(myFrame.getExtendedState());
            start();
        }  
        private void start() {
            timer = new javax.swing.Timer(1000, updateCol());
            timer.start();
        }  
        public Action updateCol() {
            return new AbstractAction("Set Delay") {  
                private static final long serialVersionUID = 1L;  
                @Override
                public void actionPerformed(ActionEvent e) {
                    timer.stop();
                    myFrame.setExtendedState(JFrame.ICONIFIED);
                    System.out.println(myFrame.getExtendedState());
                    count++;
                    startAgain();
                }
            };
        }  
        private void startAgain() {
            timer = new javax.swing.Timer(1000, updateColAgain());
            timer.start();
        }  
        public Action updateColAgain() {
            return new AbstractAction("Set Delay") {  
                private static final long serialVersionUID = 1L;  
                @Override
                public void actionPerformed(ActionEvent e) {
                    timer.stop();
                    myFrame.setExtendedState(JFrame.NORMAL);
                    System.out.println(myFrame.getExtendedState());
                    count++;
                    if (count > 5) {
                        timer.stop();
                        stop();
                    }
                    start();
                }
            };
        }  
        private void stop() {
            System.out.println("--------------------------------------------");
            System.out.println("System Exit");
            System.exit(0);
        } 
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {  
                @Override
                public void run() {
                    WindowGCDemo1 windowGCDemo = new WindowGCDemo1();
                }
            });
        }
    }
    

    EDIT 2.

    for SystemTry you have to set for your JFrame#setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); and in the proper JMenuItem(s) from JPopupMenu, just JFrame#setVisible(true);

提交回复
热议问题