我正在尝试一次在屏幕上打印一个整数数组的内容,但将先前的元素保留在屏幕上,我该怎么做?这就是我目前所拥有的,它打印每个元素,但不会将之前的元素保留在屏幕上
import javax.swing.*;
import java.awt.*;
class test extends JFrame
{
JPanel panel;
public static void main(String[] args)
{
test obj = new test();
obj.makeAnim();
}
public void makeAnim() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Animate");
setResizable(false);
setSize(400,400);
Animate arr1 = new Animate();
Animate arr2 = new Animate();
//arr1.add(arr2);
getContentPane().add(arr1);
setVisible(true);
new Thread(arr1).start();
//new Thread(arr2).start();
}
}
class Animate extends JPanel implements Runnable
{
int j = 1;
int [] a = {1,2,3,5,6,7,2,1,10,99};
String temp;
public Animate()
{
setPreferredSize(new Dimension(400,400));
}
public void run()
{
for (int i = 0; i < 10; i++) {
temp = Integer.toString(a[i]);
j++;
repaint();
try {
Thread.sleep(2000);
} catch (Exception ex) {}
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
g.setFont(new Font("Courier",Font.PLAIN, 20));
g.drawString(temp, (50+(j*10)), 50);
}
}