Java捕获窗口状态事件
Java捕获窗口状态事件 package com.han;
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import javax.swing.JFrame;
/**
* 捕获窗口状态事件
* @author HAN
*
*/
public class WindowEvent_2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = -2809729177140674307L;
public WindowEvent_2() {
// TODO Auto-generated constructor stub
addWindowStateListener(new WindowStateListener() {
@Override
public void windowStateChanged(WindowEvent e) {
// TODO Auto-generated method stub
int oldState = e.getOldState();
int newState = e.getNewState();
String from = null;
String to;
switch (oldState) {
case Frame.NORMAL:
from = "正常化";
break;
case Frame.MAXIMIZED_BOTH:
from = "最大化";
break;
case Frame.ICONIFIED:
from = "图标化";
break;
}
switch (newState) {
case Frame.NORMAL:
to = "正常化";
break;
case Frame.MAXIMIZED_BOTH:
to = "最大化";
break;
default:
to = "图标化";
break;
}
System.out.println(from + "---->" + to);
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
WindowEvent_2 frame = new WindowEvent_2();
frame.setTitle("捕获窗口状态事件"); www.
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 300, 100);
}
}