How to detect JFrame window minimize and maximize events?

后端 未结 3 1165
北荒
北荒 2021-01-02 12:05

Is there a way to an event listener to a JFrame object to detect when the user clicks the window maximize or minimize buttons?

Am using the JFrame object as follows:

相关标签:
3条回答
  • 2021-01-02 12:08

    Yes, you can do this by implementing WindowListener methods namely windowIconified(WindowEvent e) by windowDeiconified(WindowEvent e).

    For more details, visit this

    0 讨论(0)
  • 2021-01-02 12:12

    You can use WindowStateListener. How to Write Window Listeners tutorial demonstrates how to create window-related event handlers.

    0 讨论(0)
  • 2021-01-02 12:17
    1. Create a frame and add a listener:

    JFrame frame = new JFrame();
    frame.addWindowStateListener(new WindowStateListener() {
       public void windowStateChanged(WindowEvent arg0) {
          frame__windowStateChanged(arg0);
       }
    });

    1. Implement the listener:

    public void frame__windowStateChanged(WindowEvent e){
       // minimized
       if ((e.getNewState() & Frame.ICONIFIED) == Frame.ICONIFIED){
          _print("minimized");
       }
       // maximized
       else if ((e.getNewState() & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH){
          _print("maximized");
       }
    }

    0 讨论(0)
提交回复
热议问题