import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyFrame extends JFrame implements ActionListener{
String Title;
private JButton start;
private JButton stop;
private JPanel panel;
private JTextArea textArea=new JTextArea("0");
public MyFrame(String title){
this.Title=title;
start=new JButton("start");
stop=new JButton("stop");
panel=new JPanel();
panel.add(start);
panel.add(stop);
panel.add(textArea);
this.getContentPane().add(panel);
this.setVisible(true);
this.setSize(400,300);
start.addActionListener(this);
stop.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String label=e.getActionCommand();
if(label.equals("start")){
ThreadA.begin();
if(label.equals("stop")){
ThreadA.end();
}
}
}
public static void main(String args[]){
MyFrame myFrame=new MyFrame("计时器");
}
}
import java.util.*;
public class ThreadA extends Thread{
static private Date date1,date2;
public ThreadA(){
}
public static void begin(){
date1=new Date(System.currentTimeMillis());
System.out.println(date1);
}
public static void end(){
date2=new Date(System.currentTimeMillis());
System.out.println(date2);
double time=date2.getTime()-date1.getTime();
System.out.println(time);
}
public static void main(String args[]){
ThreadA thread=new ThreadA();
}
}
为什么在第二个文件写的end()方法不能用到第一个文件中呢?