Unresolved compilation problem: The method start() in the type Thread is not

Moliay 2018-11-28 11:36:30
package javaPractice;

import javax.swing.*;
import java.text.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class StopWatch1 {
public static void main(String[] args) {
Timer timer = new Timer();
Thread t = new Thread();
t.start(timer);
}
}

class Timer extends JFrame implements Runnable {
JLabel jl;
JTextField jt1,jt2;
JButton jb;
SimpleDateFormat df;
private static TimeNow thread;
private Date date;
String time;

Timer(){
this.setSize(400,300);//初始化窗口
this.setTitle("Stopwatch");
this.setLayout(null);//设置布局

df = new SimpleDateFormat("hh:mm:ss");//设置时间格式

jt1 = new JTextField();//实例化组件
jt2 = new JTextField();
jb = new JButton("Current time");


jt1.setBounds(105,50,120,35);//定义各组件的显示位置和大小
jb.setBounds(90,110,150,30);
jt2.setBounds(105,170,120,35);


this.add(jt1);//把现有组件添加到窗体窗口
this.add(jb);
this.add(jt2);

this.setLocationRelativeTo(null);//窗口居中显示
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

thread = new TimeNow();

jb.addActionListener(new ButtonListener());

jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Date d = new Date();
time = date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
jt2.setText(time);
}
});
}

class TimeNow implements Runnable{
public void run() {
while(true) {
jt1.setText(df.format(new Date()));
try {
Thread.sleep(1);
}catch(InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
}

public void start() {
jt2.setText(df.format(new Date()));
}
}

class ButtonListener implements ActionListener{
JPanel jp = new JPanel();

public void actionPerformed(ActionEvent e) {
thread.start();
}
}
public void run() {
repaint();
while(true) {
Date d = new Date();
time = date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
jt1.setText(time);
}
}
}
...全文
247 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiangxxxz 2018-11-28
  • 打赏
  • 举报
回复
main 方法 改成这样 public static void main(String[] args) { Timer timer = new Timer(); Thread t = new Thread(timer); t.start(); }

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧