本文共 1396 字,大约阅读时间需要 4 分钟。
/**
* WindowListener:窗口监听事件 ex:关闭、最小化...
* @author Administrator
*/
public class WindowListenerDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("窗口程序");//创建一个带标题的窗口对象frame
frame.setVisible(true);//显示窗口
frame.setSize(600, 400);//设置窗口大小
frame.setLocationRelativeTo(null);//设置窗口居中
//frame.setDefaultCloseOperation(3);//点击X关闭程序 3为结束程序
frame.setLayout(null);//清空布局
MyWindowListener myWindowListener = new MyWindowListener();//创建一个窗口监听事件对象
frame.addWindowListener(myWindowListener);//添加myMouseListener鼠标监听事件
frame.dispose();//关闭程序
}
}
class MyWindowListener implements WindowListener{
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
/*
* 关闭窗口之后,自动调用此方法
*/
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("您关闭了窗口...");
}
/*
* 最小化窗口之后,自动调用此方法
*/
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("您把窗口最小化了....");
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
转载地址:http://soatv.baihongyu.com/