示例:
label.addMouseListener(new MouseListener() {
public void mouseEntered(MouseEvent e) {// 光标移入组件时被触发
System.out.println("光标移入组件");
}
public void mousePressed(MouseEvent e) {// 鼠标按键被按下时被触发
System.out.print("鼠标按键被按下,");
int i = e.getButton(); // 通过该值可以判断按下的是哪个键
if (i == MouseEvent.BUTTON1)
System.out.println("按下的是鼠标左键");
if (i == MouseEvent.BUTTON2)
System.out.println("按下的是鼠标滚轮");
if (i == MouseEvent.BUTTON3)
System.out.println("按下的是鼠标右键");
}
public void mouseReleased(MouseEvent e) {// 鼠标按键被释放时被触发
System.out.print("鼠标按键被释放,");
int i = e.getButton(); // 通过该值可以判断释放的是哪个键
if (i == MouseEvent.BUTTON1)
System.out.println("释放的是鼠标左键");
if (i == MouseEvent.BUTTON2)
System.out.println("释放的是鼠标滚轮");
if (i == MouseEvent.BUTTON3)
System.out.println("释放的是鼠标右键");
}
public void mouseClicked(MouseEvent e) {// 发生单击事件时被触发
System.out.print("单击了鼠标按键,");
int i = e.getButton(); // 通过该值可以判断单击的是哪个键
if (i == MouseEvent.BUTTON1)
System.out.print("单击的是鼠标左键,");
if (i == MouseEvent.BUTTON2)
System.out.print("单击的是鼠标滚轮,");
if (i == MouseEvent.BUTTON3)
System.out.print("单击的是鼠标右键,");
int clickCount = e.getClickCount();
System.out.println("单击次数为" + clickCount + "下");
}
public void mouseExited(MouseEvent e) {// 光标移出组件时被触发
System.out.println("光标移出组件");
}
});
3.窗体事件监听
WindowFocusListener(窗体焦点监听接口)
触发动作
处理方法
窗体获得焦点时被触发
void windowGainedFocus(WindowEvent e)
窗体失去焦点时被触发
void windowLostFocus(WindowEvent e)
示例:
public class WindowFocusListener_Example extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String args[]) {
WindowFocusListener_Example frame = new WindowFocusListener_Example();
frame.setVisible(true);
}
public WindowFocusListener_Example() {
super();
// 为窗体添加焦点事件监听器
addWindowFocusListener(new MyWindowFocusListener());
setTitle("捕获窗体焦点事件");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private class MyWindowFocusListener implements WindowFocusListener {
public void windowGainedFocus(WindowEvent e) {// 窗口获得焦点时被触发
System.out.println("窗口获得了焦点!");
}
public void windowLostFocus(WindowEvent e) {// 窗口失去焦点时被触发
System.out.println("窗口失去了焦点!");
}
}
}
WindowStateListener(窗体状态监听接口)
触发动作
处理方法
窗体状态发生变化时被触发(最大化、最小化、正常之间的转换)
void windowStateChanged(WindowEvent e)
示例:
public class WindowStateListener_Example extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String args[]) {
WindowStateListener_Example frame = new WindowStateListener_Example();
frame.setVisible(true);
}
public WindowStateListener_Example() {
super();
// 为窗体添加状态事件监听器
addWindowStateListener(new MyWindowStateListener());
setTitle("捕获窗体状态事件");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
private 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);
}
}
}