为了账号安全,请及时绑定邮箱和手机立即绑定

不知道哪里错了.以下是代码!~

不知道哪里错了.以下是代码!~

回首忆惘然 2022-10-12 15:11:09
public class Time extends JFrame implements Runnable{int year = 0;int moth = 0;int day = 0;int hour = 0;int minute = 0;int second = 0;int width = Toolkit.getDefaultToolkit().getScreenSize().width;int height = Toolkit.getDefaultToolkit().getScreenSize().height;JLabel jl1 = new JLabel();Thread td = new Thread();public Time(){td.start();Calendar c=Calendar.getInstance();//获得系统当前日期year=c.get(Calendar.YEAR);moth = c.get(Calendar.MONTH)+1;//系统日期从0开始算起day=c.get(Calendar.DAY_OF_MONTH);hour = c.get(Calendar.HOUR);//小时minute =c.get(Calendar.MINUTE);//分second = c.get(Calendar.SECOND);//秒jl1 = new JLabel(year + "年" + moth + "月" + day + "日" + hour+ "时" + minute + "分" + second + "秒");jl1.setBounds(200, 100, 100, 50);this.add(jl1);this.setTitle("时间线程");this.setLocation((width - 500) / 2, (height - 500) / 2);this.setSize(500, 500);this.setVisible(true);}public static void main(String[] args) {new Time();}@Overridepublic void run() {while(true){try {td.sleep(1000);jl1 = new JLabel(year + "年" + moth + "月" + day + "日" + hour+ "时" + minute + "分" + second + "秒");} catch (InterruptedException e) {e.printStackTrace();}}}}
查看完整描述

2 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Time extends JFrame implements Runnable {

int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
JLabel jl1 = new JLabel();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");

public Time() {
jl1 = new JLabel(sdf.format(new Date()));
jl1.setBounds(200, 100, 100, 50);
this.add(jl1);
this.setTitle("时间线程");
this.setLocation((width - 500) / 2, (height - 500) / 2);
this.setSize(500, 500);
this.setVisible(true);
}

public static void main(String[] args) {
Time t = new Time();
new Thread(t).start();
}

@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
jl1.setText(sdf.format(new Date()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
}

这样比较简洁吧。


查看完整回答
反对 回复 2022-10-16
?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

不应该把run方法写在main()函数里(当然你要非写在里面也有方法),在main外面重写run方法。然后在main方法里定义个thread 对象add 这个runnable引用,然后调用thread的start方法。就行了

查看完整回答
反对 回复 2022-10-16
  • 2 回答
  • 0 关注
  • 93 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信