import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CountDown extends JFrame {//窗体类,继承JFrame
JLabel timer, count;
public CountDown() {
//Container c = getContentPane();//得到内容面板
JFrame jFrame = new JFrame("倒计时");
timer = new JLabel("时钟", JLabel.CENTER);
count = new JLabel("倒计时", JLabel.CENTER);
jFrame.add(timer);//将timer添加到内容面板中
jFrame.add(count);
timer.setFont(new Font("宋体", Font.BOLD, 50)); //设置字体
count.setFont(new Font("宋体", Font.BOLD, 50)); //设置字体
jFrame.setSize(1100, 500);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭按钮
}
public static void main(String args[]) {
CountDown countDown = new CountDown();
new MyTimer1(countDown).start();//创建线程对象,并启动
}
}
class MyTimer1 extends Thread {//线程类,继承Thread类
CountDown countDown;
MyTimer1(CountDown countDown) {
this.countDown = countDown;
}
public void run() {
while (true) {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy" + "年" + "MM" + "月" + "dd" + "日" + "HH" + "时" + "mm" + "分" + "ss" + "秒");
String s = dateformat.format(new Date());
Date end = new Date(2022 - 12 - 10);
long time = (end.getTime() - 1 - System.currentTimeMillis()) / 1000;
if (time <= 0) {
break;
}
long day = time / 3600 * 24;
long hour = time / 3600;
long minute = (time - hour * 3600) / 60;
long seconds = time - hour * 3600 - minute * 60;
StringBuilder stringBuilder = new StringBuilder();
countDown.timer.setText("现在时间:" + s);//将标签内容设置为可格式化文本
stringBuilder.append("距离北京2022年冬奥会开幕还有").append(day).append("天").append(hour).append("时").append(minute).append("分").append(seconds).append("秒");
countDown.count.setText(stringBuilder.toString());
try {
Thread.sleep(1000);//休眠1秒钟时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
你都没有给控件设置坐标啊
先别管计时了,你随便往label里写几个字,好歹能显示这个label再说改变字的问题