由于我是一个真正的编程菜鸟,我试图编写这段代码并得到部分输出。这个想法是打印在小程序上键入的字符,如果按下 alt 或 shift 键,它必须显示在状态栏中。问题是小程序上没有响应,但是在按 alt 或 shift 键时,我在状态栏上得到了适当的响应。这是代码:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/* <applet code="MyKeyApplet.class"
width = "400"
height = "400">
</applet>*/
public class MyKeyApplet extends Applet implements KeyListener{
char ch;
String str=" ";
public void init(){
this.addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent k){
int x = k.getKeyCode();
if(x==KeyEvent.VK_SHIFT)
showStatus("You pressed Shift Key");
else if (x==KeyEvent.VK_ALT)
showStatus("You pressed Alt Key");
}
public void paint(Graphics g){
g.drawString(str,40,40);
}
public void keyTyped(KeyEvent k){
ch=k.getKeyChar();
str="You Pressed" + ch;
repaint();
}
public void keyReleased(KeyEvent k){
}
}