可能重复:
希望在 Timer 生成事件时额外绘制线条
public class test4 extends JFrame implements ActionListener {
static test4 test = new test4();
Vector vec = new Vector();
Timer t = new Timer(1000, this);
int timerCount = 4;
int lineCount=1;
public static void main(String[] args) {
test.t.start(); // START THE TIMER - MAKE EVENTS EVERY 1SECONDS
test.setSize(400,400);
test.setVisible(true);
}
public void actionPerformed(ActionEvent e){
int[] nums = new int[4];
nums[0] = 0; //X1
nums[2] = 400; //X2 MAKE LINE HORIZONTALLY
nums[3] = nums[1] = 5+test.lineCount*5;// Y1,Y2 MAKE LINE HORIZONTALLY GO DOWN
vec.add(nums);
timerCount--;
//FOR TEST, LIMITED COUNTS TO 5 TIMES
if(test.timerCount<=0)
test.t.stop();
test.repaint();
}
public void paint(Graphics g){
g.setColor(Color.blue);
for(int i = 0 ; i < vec.size() ; i++)
{
int[] pos = (int[])vec.get(i);
g.drawLine(pos[0], pos[1], pos[2], pos[3]);
}
}
}
在测试中,我检查了 drawLine 的计时器和位置。这些进展顺利
但是在 JFrame 窗口中运行此代码时我看不到任何行.. :(
我应该更正代码吗?