事件处理
一、窗口事件
当用户试图关闭一个框架窗口时,JFrame对象就是窗口事件WindowEvent的事件源。
在窗口事件监听器WindowListener接口中包含7个方法,当发生窗口事件时,框架将调用这些方法来响应7个不同的事件。
public interface WindowListener
{
void windowActivated(WindowEvent e); //当窗口设置为活动窗口时调用
void windowClosed(WindowEvent e); //窗口关闭后调用,调用dispose后执行关闭
void windowClosing(WindowEvent e); //关闭窗口时调用,即窗口系统菜单中关闭,windows中右上角关闭。
void windowDeactivated(WindowEvent e); //当窗口不再是活动窗口时调用
void windowDeiconified(WindowEvent e); //窗口由最小化变为正常状态时调用
void windowIconified(WindowEvent e); //窗口由正常状态变为最小化时调用
void windowOpened(WindowEvent e); //窗口首次变为可见时调用
}
由于一个类要实现WindowListener接口时,需要实现接口WindowListener的7个方法,当然只需要一个方法时,其余的六个方法可以空操作。
当然如果每次需要用到一个方法都需要写其它六个方法空实现比较麻烦,因此对于含有多个方法的事件监听器都提供了对应的适配器。
例如:监听器WindowListener的适配器WindowAdapter类,该类是一个虚类,不可实例化,
WindowAdapter实现了WindowListener接口,只不过对于WindowListener的7个方法实现都是空操作。
因此对于需要构造的监听器可以继承WindowAdapter类,再对需要的对应方法重写即可。
class windowLclosing extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
WindowListener wListener = new windowLclosing();
然后在事件源注册事件监听器即可:
frame.addWindoListener(wListener);
当然上述可以使用匿名内部类来处理:
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
例程:
import javax.swing.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;
import java.awt.event.*;
import java.awt.geom.*;
public class WindowEventFrameTest01
{
public static void main(String[] args)
{
EventFrame sp = new EventFrame();
//sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口是操作
sp.setVisible(true); //显示组件
sp.setTitle("SimpleWindow"); //标题栏
}
}
class EventFrame extends JFrame
{
public EventFrame()
{
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.out.println("closing");
dispose(); //如果不调用dispose方法,则不会触发windowClosed方法
}
public void windowClosed(WindowEvent e)
{
System.out.println("closed");
}
public void windowDeiconified(WindowEvent e)
{
System.out.println("iconified");
EventPanel mp = new EventPanel();
add(mp);
}
});
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;
}
//按钮面板
class EventPanel extends JPanel implements ActionListener
{
public EventPanel()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.getImage("2b.jpg");
Image imgc = img.getScaledInstance(15,15,5);
ImageIcon imagI = new ImageIcon(imgc);
yelloB = new JButton("黄色",imagI); //事件源
blueB = new JButton("蓝色");
redB = new JButton("红色");
add(yelloB);
add(blueB);
add(redB);
//组件事件监听器
yelloB.addActionListener(this);
blueB.addActionListener(this);
redB.addActionListener(this);
}
//此处需要调用EventPanel的方法,所以ColorActionListener作为EventPanel的内部类
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if(source == yelloB)
setBackground(Color.YELLOW);
if(source == blueB)
setBackground(Color.BLUE);
if(source == redB)
setBackground(Color.RED);
}
private JButton yelloB ; //事件源
private JButton blueB ;
private JButton redB ;
}
常用AWT事件类型:
ActionEvent、AdjustmentEvent、FocusEvent、ItemEvent、
KeyEvent、MouseEvent、MouseWheelEvent、WindowEvent
常用事件监听接口:
ActionListener、AdjustmentListener、FocusListener、
ItemListener、KeyListener、MouseListener、MouseMotionListener、
MouseWheelListener、WindowListener、WindowFocusListener、WindowStateListener
常用事件监听接口的适配器:
FocusAdapter、KeyAdapter、MouseAdapter、MouseMotionAdapter、WindowAdapter
二、AWT的语义事件和低级事件
AWT将事件分为低级事件和语义事件。
语义事件是表达用户动作事件,例如点击按钮,产生ActionEvent语义事件。
低级事件是指形成那些事件的事件,例如:点击按钮时包含了按下鼠标、抬起鼠标等低级事件。
常用语义事件:
ActionEvent,AdjustmentEvent,ItemEvent
常用低级事件:
KeyEvent,MouseEvent,MouseWheelEvent,FocusEvent,WindowEvent
所有低级事件都继承于ComponentEvent类,其提供了getComponent()返回哪个组件产生了该事件。
三、低级事件类型
键盘事件:
虚拟键码用前缀VK_表示,例如VK_A,VK_SHIFT。虚拟键码与键盘上的键一一对应。虚拟键码没有单独的小写键,即键盘没有单独的小写键。
如下例子中,使用WASD绘制图形,并且通过大小写控制绘制的大小
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.ArrayList;
public class KeyFrameTest01
{
public static void main(String[] args)
{
KeyFrame sp = new KeyFrame();
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口是操作
sp.setVisible(true); //显示组件
sp.setTitle("KeyWindow"); //标题栏
}
}
class KeyFrame extends JFrame
{
public KeyFrame()
{
KeyPanel mp = new KeyPanel();
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); //设置组件宽度和高度
//setResizable(false); //设置组件大小是否可调节
add(mp);
}
public static final int DEFAULT_WIDTH = 650;
public static final int DEFAULT_HEIGHT = 350;
}
class KeyPanel extends JPanel
{
public KeyPanel()
{
last = new Point2D.Double(100,100);
lines = new ArrayList<Line2D>();
KeyListener kl = new KeyLener();
addKeyListener(kl);
JButton clearB = new JButton("清除");
add(clearB);
ActionListener act = new ClActionListener();
clearB.addActionListener(act);
setFocusable(true);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D gps2D = (Graphics2D)g ;
//图形颜色
gps2D.setPaint(Color.BLUE);
for(Line2D li:lines)
{
gps2D.draw(li);
}
}
public void add(int aX,int bY)
{
Point2D p = new Point2D.Double(last.getX()+aX,last.getY()+bY);
Line2D lin = new Line2D.Double(last,p);
lines.add(lin);
repaint();
last = p;
}
private class KeyLener implements KeyListener
{
public void keyPressed(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
char temp = e.getKeyChar();
int dist;
if(Character.isUpperCase(temp))
{
dist = LARGE_INCREMENT;
temp = Character.toLowerCase(temp);
}
else
{
dist = SMALL_INCREMENT;
}
if(temp == 'w') add(0,-dist);
else if(temp == 's') add(0,dist);
else if(temp == 'a') add(-dist,0);
else if(temp == 'd') add(dist,0);
}
}
class ClActionListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
lines.clear();
last = new Point2D.Double(100,100);
}
}
private JButton clearB;
private JButton reSet;
private Point2D last;
private ArrayList<Line2D> lines;
private static final int SMALL_INCREMENT =5;
private static final int LARGE_INCREMENT =10;
}
运行结果: