import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
//Applet类 实现接口 键盘事件响应器
public class Exa6_34 extends Applet implements KeyListener {
Button b;
public void init() {
setLayout(new FlowLayout());// 流布局
b = new Button("组合键改变按钮位置");// 创建一个按钮对象
b.addKeyListener(this);// 添加键盘事件响应器
add(b);// 加入到程序中
}
public void keyPressed(KeyEvent e) {// 键盘按下事件, 当按键被按下时响应
Button button = (Button) e.getSource();
int x = 0, y = 0, w = 0, h = 0;// x,y代表位置 w,h代表宽和高
x = button.getBounds().x;// 取得按钮的x坐标
y = button.getBounds().y;// 取得按钮的y坐标
w = button.getBounds().width;// 取得按钮的宽
h = button.getBounds().height;// 取得按钮的高
if (e.getModifiers() == InputEvent.SHIFT_MASK && e.getKeyCode() == KeyEvent.VK_X) {// 当键盘同时按下SHIFT和X键时
button.setLocation(y, x);// 把按钮的原位置x,y 变化成为y,x位置
button.setLabel("组合键 SHIFT_X ");// 按钮上的文字改变成为组合键 SHIFT_X
} else if (e.getModifiers() == InputEvent.CTRL_MASK && e.getKeyCode() == KeyEvent.VK_X) {// 当键盘同时按下CTRL和X键时
button.setLocation(h, w);//把按钮的高h ,宽w 设置成为 按钮的位置
button.setLabel("组合键 CTRL_X ");// 按钮上的文字改变成为组合键CTRL_X
}else if(e.getModifiers()==InputEvent.ALT_MASK&&e.getKeyCode()==KeyEvent.VK_X){// 当键盘同时按下ALT和X键时
System.exit(0);//系统退出
}
}
public void keyTyped(KeyEvent e) {//(点击按钮)键盘按下,然后释放。
}
public void keyReleased(KeyEvent e) {//按键释放,弹起
}
}