屏蔽关闭事件
public class Test extends JFrame{ public Test(){ this.setSize(300,200); //激活窗口事件 this.enableEvents(AWTEvent.WINDOW_EVENT_MASK); } @Override protected void processWindowEvent(WindowEvent e) { if (e.getID() == WindowEvent.WINDOW_CLOSING) return; //直接返回,阻止默认动作,阻止窗口关闭 super.processWindowEvent(e); //该语句会执行窗口事件的默认动作(如:隐藏) } public static void main(String[] args) { new Test().setVisible(true); } }
最小化可以用以下屏蔽:
public void windowStateChanged(WindowEvent state) { if(state.getNewState() == 1 || state.getNewState() == 7) { setExtendedState(JFrame.NORMAL);//恢复正常 }else if(state.getNewState() == 0) { //System.out.println("窗口恢复到初始状态"); }else if(state.getNewState() == 6) { //System.out.println("窗口最大化"); } }