ozuo

-------1------------------------------------------------
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.JFrame;

public class b extends JFrame{
public static void main(String args[]){
b frame = new b();
frame.setVisible(true);
}
public b(){
super();
addWindowFocusListener(new MyWindowsFocusListener());
setTitle("case");
setBounds(100,100,500,375);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private class MyWindowsFocusListener implements WindowFocusListener{

	@Override
	public void windowGainedFocus(WindowEvent e) {
		// TODO 自动生成的方法存根
		System.out.print("获得焦点!");
	}

	@Override
	public void windowLostFocus(WindowEvent e) {
		// TODO 自动生成的方法存根
		System.out.print("失去焦点!");
	}
	
}

}

------------------2----------------------
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowStateListener;

import javax.swing.JFrame;

public class b extends JFrame{
public static void main(String args[]){
b frame = new b();
frame.setVisible(true);
}
public b(){
super();
addWindowStateListener((WindowStateListener) new MyWindowStateListener());
setTitle("case");
setBounds(100,100,500,375);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public class MyWindowStateListener implements WindowStateListener{
public void windowStateChanged(WindowEvent e) {
int oldState = e.getOldState();
int newState = e.getNewState();
String from = "";
String to = "";
switch(oldState){
case Frame.NORMAL:
from = "正常化";
break;
case Frame.MAXIMIZED_BOTH:
from = "最大化";
break;
default:
from = "最小化";
}
switch(newState){
case Frame.NORMAL:
to = "正常化";
break;
case Frame.MAXIMIZED_BOTH:
to = "最大化";
break;
default:
to = "最小化";
}
System.out.println(from+"---->"+to);
}

}

}
-----------------3---------------------------
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;

import javax.swing.JFrame;

public class c extends JFrame{
public static void main(String args[]){
c frame = new c();
frame.setVisible(true);
}
public c(){
super();
addWindowListener(new MyWindowListener());
setTitle("case");
setBounds(100,100,500,375);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private class MyWindowListener implements WindowListener{

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO 自动生成的方法存根
		System.out.println("窗口被打开!");
	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO 自动生成的方法存根
		System.out.println("窗口将要被关闭!");
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO 自动生成的方法存根
		System.out.println("窗口被最小化!");
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO 自动生成的方法存根
		System.out.println("窗口被非最小化!");
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO 自动生成的方法存根
		System.out.println("窗口被激活!");
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO 自动生成的方法存根
		System.out.println("窗口不再处于激活状态!");
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO 自动生成的方法存根
		System.out.println("窗口已经被关闭!");
	}
	
}

}

posted @ 2019-11-28 09:59  杨垚1  阅读(114)  评论(0编辑  收藏  举报