源代码:
package 多线程;
- import java.awt.FlowLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- public class XianCheng extends JFrame{
- JButton jButton;
- JLabel jLabel;
- int time=8640000;// 一百天转化为秒
- public XianCheng() {
- FlowLayout fl=new FlowLayout(FlowLayout.CENTER);
- this.setLayout(fl);
- jButton=new JButton("倒数一百天 (8640000 秒)");
- // 时间倒计时
- jLabel=new JLabel();
- new Thread(){
- public void run() {
- while(time>0) {
- time--;
- jLabel.setText(time+"秒");
- try {
- Thread.sleep(1000);
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
- this.add(jButton);
- this.add(jLabel);
- this.setTitle("倒计时一百天");
- this.setBounds(700, 500, 360, 110);
- this.setResizable(true);
- this.setVisible(true);
- this.setDefaultCloseOperation(EXIT_ON_CLOSE);
- }
- public static void main(String[] args) {
- new XianCheng();
- }
- }
运行结果:
来源: http://www.bubuko.com/infodetail-3109178.html