WindowAdapter类:
1.接收窗口事件的抽象适配器类。此类中的方法为空。此类存在的目的是方便创建侦听器对象。
2.扩展此类可创建 WindowEvent
侦听器并为所需事件重写该方法。(如果要实现 WindowListener
接口,则必须定义该接口内的所有方法。此抽象类将所有方法都定义为 null,所以只需针对关心的事件定义方法。)
3.使用扩展的类可以创建侦听器对象,然后使用窗口的 addWindowListener
方法向该窗口注册侦听器。当通过打开、关闭、激活或停用、图标化或取消图标化而改变了窗口状态时,将调用该侦听器对象中的相关方法,并将WindowEvent
传递给该方法。
4.方法摘要:
方法摘要 | |
---|---|
void |
windowActivated(WindowEvent e) 激活窗口时调用。 |
void |
windowClosed(WindowEvent e) 当窗口已被关闭时调用。 |
void |
windowClosing(WindowEvent e) 窗口正处在关闭过程中时调用。 |
void |
windowDeactivated(WindowEvent e) 停用窗口时调用。 |
void |
windowDeiconified(WindowEvent e) 取消图标化窗口时调用。 |
void |
windowGainedFocus(WindowEvent e) 该 Window 被设置为聚焦 Window 时调用,聚焦 Window 意味着该 Window 或其某个子组件将接收键盘事件。 |
void |
windowIconified(WindowEvent e) 图标化窗口时调用。 |
void |
windowLostFocus(WindowEvent e) 该 Window 不再为聚焦 Window 时调用,不再为聚焦 Window 意味着键盘事件不再传递到该 Window 或其任意子组件。 |
void |
windowOpened(WindowEvent e) 已打开窗口时调用。 |
void |
windowStateChanged(WindowEvent e) 窗口状态改变时调用。 |
——摘自JDK6API
注:使用前,需使用import java.awt.enent.*;调入
例子:
例子1
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 class MyWindowListener extends WindowAdapter
5 {
6 public void windowClosing(WindowEvent e)//窗口正处在关闭过程中时调用
7 {
8 System.out.println("关闭");
9 System.exit(0);
10 }
11 }
12 public class TestWindowListener extends JFrame
13 {
14 public TestWindowListener()
15 {
16 setLayout(new FlowLayout(FlowLayout.CENTER));
17 add(new JButton("我是一个按钮"));
18 addWindowListener(new MyWindowListener());
19 }
20 public static void main(String[] agrs)
21 {
22 JFrame frame = new TestWindowListener();
23 frame.setSize(400,400);
24 frame.setLocation(400,300);
25 frame.setVisible(true);
26 }
27 }
例子2
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 public class TestWindowListener2 extends JFrame
5 {
6 public TestWindowListener2()
7 {
8 setLayout(new FlowLayout(FlowLayout.CENTER));
9 add(new JButton("我是一个按钮"));
10 addWindowListener(new WindowAdapter()
11 {
12 public void windowClosing(WindowEvent e)//窗口正处在关闭过程中时调用
13 {
14 System.out.println("我关");
15 System.exit(0);
16 }
17 }
18 );
19 }
20 public static void main(String[] agrs)
21 {
22 JFrame frame = new TestWindowListener2();
23 frame.setSize(400,400);
24 frame.setLocation(400,300);
25 frame.setVisible(true);
26 }
27 }
例子3
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 class MyWindowListener extends WindowAdapter
5 {
6 public void windowClosing(WindowEvent e)//窗口正处在关闭过程中时调用
7 {
8 System.out.println("关闭");
9 System.exit(0);
10 }
11 public void windowActivated(WindowEvent e)//激活窗口时用
12 {
13 System.out.println("哈哈,我被激活了!");
14 }
15 public void windowOpened(WindowEvent e)//已打开窗口时调用
16 {
17 System.out.println("我被打开了,哈哈,我超神了!");
18 }
19 }
20 public class TestWindowListener3 extends JFrame
21 {
22 public TestWindowListener3()
23 {
24 setLayout(new FlowLayout(FlowLayout.CENTER));
25 add(new JButton("我是一个按钮"));
26 addWindowListener(new MyWindowListener());
27 }
28 public static void main(String[] agrs)
29 {
30 JFrame frame = new TestWindowListener3();
31 frame.setSize(400,400);
32 frame.setLocation(400,300);
33 frame.setVisible(true);
34 }
35 }
package Ch08;当鼠标放到不是按钮上时: 当想关闭窗口时: 当点击按钮是:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//定义WindowListener事件
class MyWindowListener extends WindowAdapter{
//窗体关闭过程中触发函数
public void windowClosing(WindowEvent e){
new MyJDialog().setVisible(true);
System.exit(0);;
}
}
//定义关闭窗体时需要弹出的对话框
class MyJDialog extends JDialog{
public MyJDialog(){
super(new JFrame(),"提示:",true);
Container c=getContentPane();
JLabel label=new JLabel("你以为关闭就能回避这个话题吗?");
label.setHorizontalAlignment(SwingConstants.CENTER);
JButton btnY=new JButton("是");
JButton btnN=new JButton("不是");
//添加按钮监听事件
btnN.addMouseListener(new MouseListener(){
//鼠标点击、移上、移出、松开事件
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
btnN.setText("是");
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
btnN.setText("不是");
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
//添加按钮监听事件
btnN.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
label.setText("喜欢就直接说嘛。");
}
});
btnY.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
label.setText("喜欢就直接说嘛。");
}
});
btnY.setBounds(30, 100, 80, 30);
btnN.setBounds(158, 100, 80, 30);
c.add(btnY);
c.add(btnN);
c.add(label);
setSize(300, 200);
}
}
//主窗体
public class Test extends JFrame{
public void creatJFrame(String title){
JFrame jf=new JFrame(title);
Container container=jf.getContentPane();
JLabel label=new JLabel("你是不是喜欢我?");
//设置标签文字居中
label.setHorizontalAlignment(SwingConstants.CENTER);
JButton btnY=new JButton("是");
JButton btnN=new JButton("不是");
//添加按钮监听事件
btnN.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
label.setText("喜欢就直接说嘛。");
}
});
btnY.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
label.setText("喜欢就直接说嘛。");
}
});
btnN.addMouseListener(new MouseListener(){
//鼠标点击、移上、移出、松开事件
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
btnN.setText("是");
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
btnN.setText("不是");
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
btnY.setBounds(30, 100, 80, 30);
btnN.setBounds(158, 100, 80, 30);
container.add(btnY);
container.add(btnN);
container.add(label);
jf.setSize(300, 200);
jf.setVisible(true);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
jf.addWindowListener(new MyWindowListener());
}
public static void main(String[] args) {
new Test().creatJFrame("回答我:");
}
}