关于paintComponent
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class Line extends JFrame {
public Line(){
LinePanel line = new LinePanel();
add(line);
line.setFocusable(true);
}
public static void main(String []args){
Line frame = new Line();
frame.setTitle("Draw Lines");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
static class LinePanel extends JPanel{
private int x1 = 200;
private int y1 = 200;
private int x2 = x1;
private int y2 = y1;
public LinePanel(){
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_DOWN:
x2 = x1;
y2 = y1+10;
break;
case KeyEvent.VK_UP:
x2 = x1;
y2 = y1-10;
break;
case KeyEvent.VK_RIGHT:
x2 = x1+10;
y2 = y1;
break;
case KeyEvent.VK_LEFT:
x2 = x1-10;
y2 = y1;
break;
}
repaint();
}
});
}
protected void paintComponent(Graphics g){
super.paintComponents(g);
g.drawLine(x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
}
写了一段用键盘上下左右画线段的代码。可是不明白为什么用了super.paintComponents(g);
后之前画的线段能继续显示 不是说super.paintComponents(g);会起到清屏重新绘制的作用吗?求详细解惑 谢了~~~~~~~
[解决办法]
看看你的结贴率,让回帖的人情何以堪啊!
[解决办法]
该回复于2012-11-18 23:21:07被管理员删除