Java窗口的监听<wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px">可以在</span><span style="color:#0000ff; line-height:25px">window</span><span style="color:#003366; line-height:25px">(及子类Frame,JFrame,JWindow)中使用</span><wbr style="line-height:25px"><span style="color:#993300; line-height:25px">addWindowListener</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">方法来添加</span><wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">WindowListener</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">实现都窗口事件的监听。</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicinterface</span><span style="color:#003366; line-height:25px"></span><span style="color:#ff6600; line-height:25px">WindowListener</span><span style="color:#003366; line-height:25px"></span><span style="color:#993300; line-height:25px">extend</span><span style="color:#003366; line-height:25px">s</span><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">EventListener</span><br style="line-height:25px"><div style="line-height:25px"> <span style="line-height:25px; color:rgb(255,102,0)">WindowListener</span><span style="color:#003366; line-height:25px">用于接收窗口事件的侦听器接口。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">旨在处理窗口事件的类</span><span style="color:#000080; line-height:25px">要么实现此接口(及其包含的所有方法),要么扩展抽象类WindowAdapter</span><span style="color:#003366; line-height:25px">(仅重写所需的方法)。</span><br style="line-height:25px"><wbr style="line-height:25px"><span style="color:#003366; line-height:25px">然后使用窗口的</span><span style="color:#993300; line-height:25px">addWindowListener</span><span style="color:#003366; line-height:25px">方法将从该类所创建的侦听器对象向该Window注册。</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">当通过</span><span style="color:#0000ff; line-height:25px">打开、关闭、激活或停用、图标化</span><span style="color:#003366; line-height:25px">或</span><span style="color:#0000ff; line-height:25px">取消图标化</span><span style="color:#003366; line-height:25px">而改变了窗口状态时,将调用该侦听器对象中的相关方法,</span><span style="color:#003366; line-height:25px">并将WindowEvent传递给该方法。</span><br style="line-height:25px"> 方法摘要<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">voidwindowActivated(WindowEvente)</span><br style="line-height:25px"> 将Window设置为活动Window时调用。<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">voidwindowClosed(WindowEvente)</span><br style="line-height:25px"> 因对窗口调用dispose而将其关闭时调用。<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">voidwindowClosing(WindowEvente)</span><br style="line-height:25px"> 用户试图从窗口的系统菜单中关闭窗口时调用。<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">voidwindowDeactivated(WindowEvente)</span><br style="line-height:25px"> 当Window不再是活动Window时调用。<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">voidwindowDeiconified(WindowEvente)</span><br style="line-height:25px"> 窗口从最小化状态变为正常状态时调用。<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">voidwindowIconified(WindowEvente)</span><br style="line-height:25px"> 窗口从正常状态变为最小化状态时调用。<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">voidwindowOpened(WindowEvente)</span><br style="line-height:25px"> 窗口首次变为可见时调用。<br style="line-height:25px"><wbr style="line-height:25px"><span style="line-height:25px">注意:</span><span style="color:#000080; line-height:25px">windowClosed只有会在dispose而将其关闭时调用<wbr style="line-height:25px">。即使点击关闭按钮,程序关闭,该函数也没调用<wbr style="line-height:25px">。</wbr></wbr></span></wbr></wbr> </div> <div style="line-height:25px"> <span style="color:#000080; line-height:25px"><br style="line-height:25px"></span>可以在<span style="color:#0000ff; line-height:25px">window</span>(及子类Frame,JFrame,JWindow)中使用<span style="color:#993300; line-height:25px">addWindowStateListener</span>方法来添加<span style="color:#ff6600; line-height:25px">WindowStateListener</span>。<br style="line-height:25px"> 实现都窗口状态的监听。<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicinterface</span><span style="color:#ff6600; line-height:25px">WindowStateListener</span><span style="color:#993300; line-height:25px">extends</span>EventListener</div> <div style="line-height:25px"> <span style="line-height:25px; color:rgb(255,102,0)">WindowStateListener</span>用于接收窗口状态事件的侦听器接口。<br style="line-height:25px"><span style="color:#003366; line-height:25px">旨在处理窗口状态事件的类</span><span style="color:#000080; line-height:25px">要么实现此接口(及其包含的所有方法),要么扩展抽象WindowAdapter类</span><span style="color:#003366; line-height:25px">(仅重写所需的方法)。<br style="line-height:25px"> 然后使用Window的</span><span style="color:#993300; line-height:25px">addWindowStateListener</span><span style="color:#003366; line-height:25px">方法将从该类所创建的侦听器对象向该Window注册。<br style="line-height:25px"> 当通过图标化、最大化等改变窗口状态时,就调用侦听器对象中的windowStateChanged方法,并将WindowEvent传递给该方法。</span><br style="line-height:25px"> 方法摘要<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px"></wbr></span><span style="color:#993300; line-height:25px">void</span><span style="color:#ff6600; line-height:25px">windowStateChanged</span>(WindowEvente)<br style="line-height:25px"><wbr style="line-height:25px">窗口状态改变时调用。<br style="line-height:25px"><span style="color:#99cc00; line-height:25px">JFrame</span><span style="color:#000080; line-height:25px">与</span><span style="color:#808000; line-height:25px">Frame</span><span style="color:#000080; line-height:25px">不同,当用户试图关闭窗口时,</span><wbr style="line-height:25px"><span style="color:#99cc00; line-height:25px">JFrame</span><span style="color:#000080; line-height:25px">知道如何进行响应<wbr style="line-height:25px">。<br style="line-height:25px"> 用户关闭窗口时,默认的行为只是简单地隐藏</wbr></span><span style="color:#99cc00; line-height:25px">JFrame</span><span style="color:#000080; line-height:25px">。要更改默认的行为,可调用方法</span><span style="color:#0000ff; line-height:25px">setDefaultCloseOperation(int)</span><span style="color:#000080; line-height:25px">。<br style="line-height:25px"> 要使JFrame的行为与Frame实例相同,请使用</span><span style="color:#0000ff; line-height:25px">setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)</span><span style="color:#000080; line-height:25px">。</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">如果要点击关闭按钮就退出程序请调用</span><wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px">setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span></wbr></wbr></wbr> </div> </wbr></wbr></wbr></wbr></wbr></wbr>