Swing设计的GUI(Graphical User Interface)组件非常灵活,在GUI界面上所做的任何操作都是事件,如键盘输入、选择某个单个按钮、移动鼠标等都被GUI当作事件捕获,并处理该事件。GUI事件处理机制是建立交互式应用程序的关键技术。各种事件的业务处理由相应的监听器完成,如在窗体上单击鼠标按键,将会由指定的鼠标监听器来捕获这个操作,并做出相应的处理。
1、KeyEvent
按键事件监听 implements KeyListener
package Fourteen;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
public class KeyEvent extends JFrame{
private JTextField textField1;
private JTextField textField2;
public static void main(String[] args){
KeyEvent frame = new KeyEvent();
frame.setVisible(true);
}
public KeyEvent(){
super();
getContentPane().setLayout(null);
setBounds(100,100,305,113);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField1 = new JTextField();
textField1.addKeyListener(new TextKeyListener());
textField1.setBounds(115,12,172,22);
getContentPane().add(textField1);
textField2 = new JTextField();
/*addKeyListener()方法用于向组件注册按键时间的监听器,当用户
* 在组件上按下键盘上的按键时,将产生KeyEvent类型事件对象,这个
* 事件将被注册到自建上的监听器捕获,并由监听器指定的方法来处理*/
textField2.addKeyListener(new TextKeyListener());
textField2.setBounds(115,40,172,22);
getContentPane().add(textField2);
final JLabel label = new JLabel();
label.setText("第一个文本");
label.setBounds(10,12,99,18);
getContentPane().add(label);
final JLabel label_1 = new JLabel();
label_1.setText("第二个文本");
label_1.setBounds(10,40,99,18);
getContentPane().add(label_1);
}
private final class TextKeyListener implements KeyListener{
//输入字符时
public void keyTyped(java.awt.event.KeyEvent e) {
// TODO Auto-generated method stub
}
//键盘按键按下时
public void keyPressed(java.awt.event.KeyEvent e) {
// TODO Auto-generated method stub
}
//键盘按键抬起时
public void keyReleased(java.awt.event.KeyEvent e) {
// TODO Auto-generated method stub
String text = textField1.getText();
textField2.setText(text);
}
}
}
2、窗体事件 WindowListener
窗体在系统中有不同的状态,如打开窗口处于激活状态,选择其他窗口时该窗口处于未激活状态,最小化、关闭……
package Fourteen;
import javax.swing.JFrame;
import java.awt.event.WindowListener;
public class WindowEvent extends JFrame{
public static void main(String[] args){
WindowEvent frame = new WindowEvent();
frame.setVisible(true);
}
public WindowEvent(){
super();
addWindowListener(new WindowListener(){
public void windowOpened(java.awt.event.WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("Open");
}
public void windowClosing(java.awt.event.WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("Closing");
}
public void windowClosed(java.awt.event.WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("Closed");
}
public void windowIconified(java.awt.event.WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("最小化");
}
public void windowDeiconified(java.awt.event.WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("脱离最小化");
}
public void windowActivated(java.awt.event.WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("Activiate");
}
public void windowDeactivated(java.awt.event.WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("Deactiviate");
}
});
}
}
3、鼠标事件处理MouseListener
鼠标移动、单击、拖动……
package Fourteen;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.event.MouseListener;
import javax.swing.JTextField;
public class MouseEvent extends JFrame{
private JTextField textField;
public static void main(String[] args){
MouseEvent frame = new MouseEvent();
frame.setVisible(true);
}
public MouseEvent(){
super();
addMouseListener(new MouseListener(){
public void mouseClicked(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
String str = textField.getText();
textField.setText(str + ",用户连续点击了鼠标"+e.getClickCount()+"次");
}
public void mousePressed(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
textField.setText("鼠标在窗体区按下"+e.getButton());
}
public void mouseReleased(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
textField.setText("鼠标在窗体区释放"+e.getButton());
}
public void mouseEntered(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
textField.setText("鼠标进入窗体");
}
public void mouseExited(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub
textField.setText("鼠标离开窗体");
}
});
setBounds(100,100,500,375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textField = new JTextField();
getContentPane().add(textField,BorderLayout.SOUTH);
}
}