在我的
代码中,我希望方法paintComponent1(画一条线)根据是否按下键来运行。我该怎么做呢?感谢所有帮助,谢谢:D!
这是我的代码:
import java.awt.event.KeyEvent;
import java.awt.*;
import java.applet.Applet;
public class Test {
boolean x = false;
public void paintComponent1(Graphics g){
g.drawLine(35, 60, 100, 120);
}
public void keyPressed(KeyEvent e) {
x = true;
}
public void keyReleased(KeyEvent e) {
x = false;
}
public void paintComponent(Graphics g) {
if (x)
paintComponent1();
}
}
为什么不将paintComponent1()移动到keyPressing方法中呢?
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_SPACE) // if you press the space bar key
{
paintComponent1();
}
}