How to detect JFrame window minimize and maximize events?

ぃ、小莉子 提交于 2019-11-29 19:03:41

问题


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:

JFrame frame = new JFrame("Frame");


回答1:


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




回答2:


  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");
   }
}



回答3:


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

For more details, visit this



来源:https://stackoverflow.com/questions/11148950/how-to-detect-jframe-window-minimize-and-maximize-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!