我正在尝试使用for循环在AWT中打印出我的ArrayList的内容。但是,当我尝试编译代码并运行它时,没有显示任何内容。我对Java很新,所以我不确定是什么导致了这一点。
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.time.*;
import javax.swing.*;
import java.util.ArrayList;
public class TimeDisplay extends Applet {
private City WashingtonDC, Beijing, London, Tokyo, Moscow, TimeRetrieval;
String temp;
String []cities = {"WashingtonDC", "Beijing", "London", "Tokyo", "Moscow"};
String [] GMT = {"-4", "+8", "+1", "+9", "+3"};
ArrayList DisplayStrings;
Boolean displaytype;
public void init() {
WashingtonDC = new City("-4", "Washington D.C.");
Beijing = new City("+8", "Beijing");
London = new City("+1", "London");
Tokyo = new City("+9", "Tokyo");
Moscow = new City("+3", "Moscow");
displaytype = false;
DisplayStrings = new ArrayList();
//timer
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (displaytype = true) {
for (int i=0; i<GMT.length; i++) {
DisplayStrings.add(TimeRetrieval.GetCurrentTime12(GMT[i]).toString());
}
}
else {
for (int i=0; i<GMT.length; i++) {
DisplayStrings.add(i, TimeRetrieval.GetCurrentTime(GMT[i]).toString());
}
}
repaint();
}
};
new Timer(delay, taskPerformer).start();
}
public void paint (Graphics g) {
for (int i=0; i<cities.length; i++) {
g.drawString(Arrays.deepToString(DisplayStrings.get(i))(), 25, (i+1)*50+25);
}
for (int i=0; i<cities.length; i++) {
g.drawString(cities[i], 25, (i+1)*50);
}
}
}