📄 keyevent3.java
字号:
import java.awt.*;
import java.awt.event.*;
public class KeyEvent3 extends Frame{
Button man;
public KeyEvent3(String title){
super(title);
setLayout(null);
this.setSize(200,200);
man=new Button("Man");
man.setBounds(100,100,40,20);
man.setBackground(Color.BLUE);
man.setForeground(Color.WHITE);
this.add(man);
man.addKeyListener(new KeyHandler());
}
public static void main(String[] args){
KeyEvent3 me=new KeyEvent3("Key 捞亥飘 劝侩");
me.setVisible(true);
}
class KeyHandler extends KeyAdapter{
public void keyPressed(KeyEvent e){
String direction=e.getKeyText(e.getKeyCode());
int x=man.getX();
int y=man.getY();
if(direction.equals("Right")) x+=10;
else if(direction.equals("Left")) x-=10;
else if(direction.equals("Down")) y+=10;
else if(direction.equals("Up")) y-=10;
man.setLocation(x,y);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -