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()
{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()
{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();
}
}
----------------解决方案--------------------------------------------------------
为什么开始时间和结束时间 在点击按钮后不改变时间了呀???
----------------解决方案--------------------------------------------------------
哪有这么容易
----------------解决方案--------------------------------------------------------
冰兄.为什么不会改变啊.解释下呀
----------------解决方案--------------------------------------------------------
哎呀.java难学啊.大家帮忙吧.我一直在等.先说下其中的原因.
----------------解决方案--------------------------------------------------------
各位帮忙啊
----------------解决方案--------------------------------------------------------
你的time每次重新生成
就会变了
----------------解决方案--------------------------------------------------------
听不懂啊..
能帮我简单改下吗
----------------解决方案--------------------------------------------------------
是啊 好奇怪
----------------解决方案--------------------------------------------------------
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 time0=new GregorianCalendar();
string="开始是"+time0.get(GregorianCalendar.YEAR)+"年"+time0.get(GregorianCalendar.MONTH)+"月"+time0.get(GregorianCalendar.DATE)+"日"+time0.get(GregorianCalendar.HOUR)+":"+time0.get(GregorianCalendar.MINUTE)+":"+time0.get(GregorianCalendar.SECOND);
label.setText(string);System.out.println(string);
}
void abc()
{
GregorianCalendar time1=new GregorianCalendar();
string="结束是"+time1.get(GregorianCalendar.YEAR)+"年"+time1.get(GregorianCalendar.MONTH)+"月"+time1.get(GregorianCalendar.DATE)+"日"+time1.get(GregorianCalendar.HOUR)+":"+time1.get(GregorianCalendar.MINUTE)+":"+time1.get(GregorianCalendar.SECOND);
label.setText(string);System.out.println(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();
}
}
//哈哈 搞定啦
----------------解决方案--------------------------------------------------------