//目标:在窗体中显示一个倒计时
import java.awt.Graphics;
import javax.swing.JFrame;
public class DaoJiShi {
public static void main(String[] args) {
new MyFrame("倒计时");
}
}
class MyFrame extends JFrame implements Runnable {
int time = 10;
String s = "无限制";
public MyFrame (String s) {
super(s);
this.setSize(200, 100);
this.setLocation(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
new Thread(this).start();
}
public void paint (Graphics g) {
g.drawString(s, 50, 50);
}
public void run() {
while (true) {
s = time + "s";
time--;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
if (time == 0) {
System.exit(0);
}
}
}
} |
|