实验十四:线程设计

源代码:

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();
}
}

 

 

运行结果:

实验十四:线程设计

 

上一篇:java – FlowLayout不流动JScrollPane


下一篇:java-为什么我的组件没有包装在Swing的FlowLayout中?