运行结果如下:
由于无法插入视频,故就只能放几张截图
源码如下:
package daojishi; import java.awt.FlowLayout; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; class TIME extends JFrame{ int day =100; int seconds=day*24*60*60; private JLabel text; public void run() { //创建run的线程函数,实现线程功能 while (seconds > 0) { seconds--; int days=seconds/60/60/24; int hours= seconds/60/60%24; int minutes= seconds/60%60; int second=seconds%60; System.out.println(days+"天"+hours+"时"+minutes+"分"+second+"秒"); try { this.text.setText(days+"天"+hours+"时"+minutes+"分"+second+"秒"); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }} public TIME(){ this.setLayout(new FlowLayout()); this.setTitle("Introduction"); this.setSize(320,200); this.setLocation(300,240); this.add(new JLabel("距离奥运会开始还剩")).setFont(new Font("黑体", 1, 20)); //这里设置了字体 Font(字体,字体样式,字体大小) this.text=new JLabel("100天00时00分00秒"); this.add(text).setFont(new Font("黑体", 1, 30)); this.setVisible(true);} } public class Daojishi { public static void main(String[] args) { new TIME().run(); //运行线程 } }
总结心得:
(1)通过该实验,让我学会了线程的创建和使用,即编写线程run函数
(2)要注意可视化界面和线程之间的结合关系,在线程中要改变JLabel的值,通过图形界面中创建标签:this.text=new JLabel,线程中调用this.text.setText来改变标签的内容
原文:https://www.cnblogs.com/fjcy/p/11099633.html