我从 Java 开始,但我遇到了问题。当人们在 java 小程序上单击“a”时,我希望它绘制一个黄色矩形,如果他们按其他任何东西,它会绘制一个黑色矩形,但什么也没有发生。
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class guitarGame extends Applet implements ActionListener, KeyListener{
Timer timer = new Timer (1000, this);
String s = "";
char a;
int selection;
public void keyReleased(KeyEvent ae){}
public void keyPressed(KeyEvent ae){}
public void keyTyped(KeyEvent ae){
a = ae.getKeyChar();
}
public void init(){
addKeyListener(this);
}
public void actionPerformed (ActionEvent ae)
{
if (a == a)
{
selection = 1;
}
else{
selection = 2;
}
}
public void paint (Graphics g)
{
if (selection == 1){
g.setColor(Color.YELLOW);
g.fillRect(100,100,100,100);
}
if (selection == 2){
g.setColor(Color.YELLOW);
g.fillRect(100,100,100,100);
}
repaint();
}
}
有什么帮助吗?