1.动作事件处理
- 动作事件由ActionEvent类定义,最常用的是当单击按钮后将产生动作事件,可以通过实现ActionListener接口处理相应的动作事件。
- ActionListener接口只有一个抽象方法,将在动作发生后被触发,例如单击按钮之后,ActionListener接口的具体定义如下:
- ActionEvent类中有两个比较常用的方法:
(1)getSource():用来获得触发此次事件的组件对象,返回值类型为Object;
(2)getActionCommand():用来获得与当前动作相关的命令字符串,返回值类型为String。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ActionEvent_Example extends JFrame {
private JLabel label;// 声明一个标签对象,用来显示提示信息
public static void main(String args[]) {
ActionEvent_Example frame = new ActionEvent_Example();
frame.setVisible(true);
}
public ActionEvent_Example() {
super("动作事件示例");
setBounds(100, 100,300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("欢迎登录!");
label.setHorizontalAlignment(JLabel.CENTER);
getContentPane().add(label);
final JButton submitButton = new JButton("登录");
submitButton.addActionListener(new ButtonAction()); // 为按钮添加动作监听器
add(submitButton, BorderLayout.SOUTH);
}
class ButtonAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource(); // 获得触发此次事件的按钮对象
String buttonName = e.getActionCommand(); // 获得触发此次事件的按钮的标签文本
if (buttonName.equals("登录")) {
label.setText("您已经成功登录!"); // 修改标签的提示信息
button.setText("退出"); // 修改按钮的标签文本
} else {
label.setText("您已经安全退出!"); // 修改标签的提示信息
button.setText("登录"); // 修改按钮的标签文本
}
}
}
}
显示效果:
(初次运行时效果) (单击“登陆”后效果) (单击“退出”后效果)
2.焦点事件处理
- 焦点事件由FocusEvent类捕获,所有的组件都能产生焦点事件,可以通过实现FocusListener接口处理相应的动作事件。
- FocusListener接口有两个抽象方法,分别在组件获得或失去焦点时被触发,FocusListener接口的具体定义如下:
- FocusEvent类中比较常用的方法是getSource(),用来获得触发此次事件的组件对象,返回值类型为Object。
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class FocusEvent_Example extends JFrame {
private JTextField textField;
public static void main(String args[]) {
FocusEvent_Example frame = new FocusEvent_Example();
frame.setVisible(true);
}
public FocusEvent_Example() {
super("焦点事件示例");
setBounds(100, 100, 300, 100);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel();
label.setText("出生日期:");
add(label);
textField = new JTextField();
textField.setColumns(10);
textField.addFocusListener(new TextFieldFocus()); // 为文本框添加焦点监听器
add(textField);
final JButton button = new JButton("确定");
add(button);
}
class TextFieldFocus implements FocusListener {
public void focusGained(FocusEvent e) {
textField.setText("");
}
public void focusLost(FocusEvent e) {
textField.setText("2008-8-8");
}
}
}
显示效果:
(文本框获得焦点)
(文本框失去焦点)
分析:运行程序,得到窗体,此时的文本框拥有焦点,即可向文本框中输入内容;单击“确定”按钮后,此时按钮将会获得焦点,而文本框会失去焦点,文本框的监听器将为文本框设置内容“2008-8-8”;如果用鼠标单击文本框,则文本框将再次得到焦点,文本框的监听器把文本框设置为空。
3.鼠标事件处理
- 鼠标事件由MouseEvent类捕获,所有的组件都能产生鼠标事件,可以通过实现MouseListener接口处理相应的鼠标事件。
- MouseListener接口有5个抽象方法,分别在光标移入(出)组件时、鼠标按键被按下(释放)时和发生单击事件时被触发。
- 所谓单击事件,就是按键被按下并释放。需要注意的是,如果按键是在移出组件之后才被释放,则不会触发单击事件。
- MouseListener接口的具体定义如下:
-
MouseEvent类中比较常用的方法如下表所示。
-
可以通过下表中的静态常量,判断通过getButton()方法得到的值代表哪个键。
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JLabel; public class MouseEvent_Example extends JFrame { public static void main(String args[]) { MouseEvent_Example frame = new MouseEvent_Example(); frame.setVisible(true); } public MouseEvent_Example() { super("鼠标事件示例"); setBounds(100, 100, 300, 100); setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JLabel label = new JLabel("hello!"); label.addMouseListener(new MouseListener() { public void mouseEntered(MouseEvent e) { System.out.println("光标移入组件"); } public void mousePressed(MouseEvent e) { System.out.println("鼠标按键被按下"); 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.println("鼠标按键被释放"); 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.println("单击了鼠标按键"); 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("单击的是鼠标右键"); int clickCount = e.getClickCount(); System.out.println("单击次数为" + clickCount + "下"); } public void mouseExited(MouseEvent e) { System.out.println("光标移出组件"); } }); add(label, BorderLayout.CENTER); } }
4.键盘事件处理
- 键盘事件由KeyEvent类捕获,最常用的是当向文本框输入内容时将发生键盘事件,可以通过实现KeyListener接口处理相应的键盘事件.
- KeyListener接口有3个抽象方法,分别在发生击键事件、键被按下和释放时被触发,Focus Listener接口的具体定义如下:
- KeyEvent类中比较常用的方法如下表所示:
-
在KeyEvent类中以“VK_”开头的静态常量代表各个按键的keyCode,可以通过这些静态常量判断事件中的按键,以及获得按键的标签。
import java.awt.BorderLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class KeyEvent_Example extends JFrame { public static void main(String args[]) { KeyEvent_Example frame = new KeyEvent_Example(); frame.setVisible(true); } public KeyEvent_Example() { super("键盘事件示例"); setBounds(100, 100, 300, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JLabel label = new JLabel("备注:"); add(label, BorderLayout.WEST); final JScrollPane scrollPane = new JScrollPane(); add(scrollPane, BorderLayout.CENTER); JTextArea textArea = new JTextArea(); textArea.addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { String keyText = KeyEvent.getKeyText(e.getKeyCode()); if (e.isActionKey()) System.out.println("您按下的是动作键“" + keyText + "”"); else { System.out.println("您按下的是非动作键“" + keyText + "”"); char keyChar = e.getKeyChar(); switch (keyChar) { case KeyEvent.VK_CONTROL: System.out.println("Ctrl键被按下"); break; case KeyEvent.VK_ALT: System.out.println("Alt键被按下"); break; case KeyEvent.VK_SHIFT: System.out.println("Shift键被按下"); break; } } } public void keyTyped(KeyEvent e) { System.out.println("此次输入的是“" + e.getKeyChar() + "”"); } public void keyReleased(KeyEvent e) { String keyText = KeyEvent.getKeyText(e.getKeyCode()); System.out.println("您释放的是“" + keyText + "”键"); System.out.println(); } }); textArea.setLineWrap(true); textArea.setRows(3); textArea.setColumns(15); scrollPane.setViewportView(textArea); } }
显示效果: