本文共 3019 字,大约阅读时间需要 10 分钟。
展开全部
以前写的,功能差不多,用1,2,3,4。。代替了。自己看一下吧
ctrl+1 表示选择编号为1的按钮32313133353236313431303231363533e4b893e5b19e31333337613264。上下键控制位置变化
package eventDemo;
import java.awt.FlowLayout;
import java.awt.KeyEventPostProcessor;
import java.awt.KeyboardFocusManager;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class KeyEventDemo implements KeyListener{
JButton []b = new JButton[10];
int x,y;
JFrame jf;
KeyboardFocusManager manager;
public static void main(String []args){
new KeyEventDemo();
}
KeyEventDemo(){
manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();//看这里。。。。。。。。。。。。。。。改成setCurrentKeyboardFocusManager(),其他在改一下就可以了
manager.addKeyEventPostProcessor(new KeyEventPostProcessor() {
@Override
public boolean postProcessKeyEvent(KeyEvent e) {
// TODO Auto-generated method stub
if ((e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) !=0){
if (e.getKeyCode()==KeyEvent.VK_1){
//System.out.println("11");
b[1].requestFocus();
}
if (e.getKeyCode()==KeyEvent.VK_2){
System.out.println("11");
b[2].requestFocus();
}
if (e.getKeyCode()==KeyEvent.VK_3){
System.out.println("11");
b[3].requestFocus();
}
if (e.getKeyCode()==KeyEvent.VK_4){
System.out.println("11");
b[4].requestFocus();
}
if (e.getKeyCode()==KeyEvent.VK_5){
System.out.println("11");
b[5].requestFocus();
}
if (e.getKeyCode()==KeyEvent.VK_6){
System.out.println("11");
b[6].requestFocus();
}
if (e.getKeyCode()==KeyEvent.VK_7){
System.out.println("11");
b[7].requestFocus();
}
if (e.getKeyCode()==KeyEvent.VK_8){
System.out.println("11");
b[8].requestFocus();
}
if (e.getKeyCode()==KeyEvent.VK_9){
System.out.println("11");
b[9].requestFocus();
}
if (e.getKeyCode()==KeyEvent.VK_0){
System.out.println("11");
b[0].requestFocus();
}
}
return false;
}
});
jf = new JFrame("KeyEventDemo");
jf.setLayout(new FlowLayout());
for (int i = 0; i <=9; i++) {
b[i] = new JButton(i+"");
jf.add(b[i]);
b[i].addKeyListener(this);
}
jf.setBounds(600,200,200,200);
jf.setVisible(true);
jf.validate();
jf.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_0)
System.out.println('0');
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
});
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
JButton temp = (JButton) e.getSource();
x = temp.getBounds().x;
y = temp.getBounds().y;
//System.out.println(x*y);
if (e.getKeyCode() == KeyEvent.VK_UP)
y = y - 20;
if (e.getKeyCode() == KeyEvent.VK_DOWN)
y = y + 20;
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
x = x + 20;
if (e.getKeyCode() == KeyEvent.VK_LEFT)
x = x - 20;
temp.setLocation(x, y);
}
}
转载地址:http://uoiox.baihongyu.com/