我试图使“剩余时间”附近的整数减一并每秒重新绘制一次,但我只能在不断调整窗口大小时看到变化。我已经在 Windows 中的 Jcreator 3 和 Ububtu 11.10 命令行中尝试过这个,但它仍然不起作用。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.*;
class StatusPanel extends JPanel implements ActionListener {
int time = 60;
int round = 1;
Timer timer;
public StatusPanel() {
this.timer = new Timer(1000, this);
this.timer.start();
this.setVisible(true);
this.setDoubleBuffered(true);
}
public void updateTime() {
if (time > 0) {
time--;
}
}
public void paint(Graphics page) {
super.paint(page);
page.setFont(new Font("TimesRoman", Font.PLAIN, 30));
String sTime = String.valueOf(time);
String sRound = String.valueOf(round);
page.setColor(Color.WHITE);
page.drawString(sTime, 253, 149);
page.drawString(sRound, 230, 105);
}
public void actionPerformed(ActionEvent event) {
this.updateTime();
}
}