用时间时需要再调用new GregorianCalendar(); 之后才存入最新的。
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class Clock2 extends Frame implements ActionListener{
private Button start=new Button("开始");
private Button stop=new Button("停止");
//private clocker clock=new clocker(this);
Label label=new Label("");
private Panel pan=new Panel();
private GregorianCalendar time=new GregorianCalendar();
private String string;
Clock2(){
string="现在是"+time.get(GregorianCalendar.YEAR)+"年"+time.get(GregorianCalendar.MONTH)+"月"+time.get(GregorianCalendar.DATE)+"日"+time.get(GregorianCalendar.HOUR)+":"+time.get(GregorianCalendar.MINUTE)+":"+time.get(GregorianCalendar.SECOND);
start.addActionListener(this);
stop.addActionListener(this);
pan.add(start);
pan.add(stop);
label.setText(string);
add(label,"North");
add(pan,"Center");
addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0);}});
show();
pack();
}
void ab()
{
GregorianCalendar time=new GregorianCalendar(); /*添加的部分*/
string="开始是"+time.get(GregorianCalendar.YEAR)+"年"+time.get(GregorianCalendar.MONTH)+"月"+time.get(GregorianCalendar.DATE)+"日"+time.get(GregorianCalendar.HOUR)+":"+time.get(GregorianCalendar.MINUTE)+":"+time.get(GregorianCalendar.SECOND);
label.setText(string);System.out.println(string);
}
void abc()
{
GregorianCalendar time=new GregorianCalendar(); /*添加的部分*/
string="结束是"+time.get(GregorianCalendar.YEAR)+"年"+time.get(GregorianCalendar.MONTH)+"月"+time.get(GregorianCalendar.DATE)+"日"+time.get(GregorianCalendar.HOUR)+":"+time.get(GregorianCalendar.MINUTE)+":"+time.get(GregorianCalendar.SECOND);
label.setText(string);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==start)
this.ab();
else if(e.getSource()==stop)
this.abc();
}
public static void main(String[] args){
Clock2 clock2=new Clock2();
}
}
/*加了两行代码,呵呵,不建议局部变量和全局变量同名*/