求解释一个JAVA(秒表)的小程序,可以给代码标记注释的。新手,看不懂代码- -。
importjava.awt.*;importjava.awt.event.*;importjava.applet.*;importjava.util.*;publicc...
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class TimeViewer extends Applet implements ActionListener,Runnable
{
//所需要的数据定义
Thread timer;
TextField in,out;
Button bb;
Panel p1,p2,p3;
boolean state;
public void init ()
{
in = new TextField (20);
out = new TextField (20);
bb = new Button ("Current Time:");//设置出按钮
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
setLayout(new GridLayout(3,1));
setSize(200,100);
p1.add(in);
p2.add(bb);
p3.add(out);
add(p1);
add(p2);
add(p3);
bb.addActionListener(this);
timer = new Thread(this);
state = true;
timer.start();
}
public void actionPerformed (ActionEvent e)
{
//out.setText(in.getText());
out.setText(currentTime());
}
public void run()
{
while (true)
{
try
{
timer.sleep(1000);
}
catch(InterruptedException e){}
in.setText(currentTime());;
}
}
String currentTime()
{
Date now = new Date();
String str = now.getHours() + ":" +now.getMinutes() + ":" +now.getSeconds();
return str;
}
} 展开
import java.awt.event.*;
import java.applet.*;
import java.util.*;
public class TimeViewer extends Applet implements ActionListener,Runnable
{
//所需要的数据定义
Thread timer;
TextField in,out;
Button bb;
Panel p1,p2,p3;
boolean state;
public void init ()
{
in = new TextField (20);
out = new TextField (20);
bb = new Button ("Current Time:");//设置出按钮
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
setLayout(new GridLayout(3,1));
setSize(200,100);
p1.add(in);
p2.add(bb);
p3.add(out);
add(p1);
add(p2);
add(p3);
bb.addActionListener(this);
timer = new Thread(this);
state = true;
timer.start();
}
public void actionPerformed (ActionEvent e)
{
//out.setText(in.getText());
out.setText(currentTime());
}
public void run()
{
while (true)
{
try
{
timer.sleep(1000);
}
catch(InterruptedException e){}
in.setText(currentTime());;
}
}
String currentTime()
{
Date now = new Date();
String str = now.getHours() + ":" +now.getMinutes() + ":" +now.getSeconds();
return str;
}
} 展开
1个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
//--------以上部分为导入需要的文件
public class TimeViewer extends Applet implements ActionListener,Runnable
{
//所需要的数据定义
Thread timer;//定义一个线程,用于每一秒去更新一次时间文字
TextField in,out;//定义两个文本框
Button bb;//定义一个按钮,用于开始/停止
Panel p1,p2,p3;//三个panel,摆放组件
boolean state;//当前状态的布尔值
public void init ()
{
in = new TextField (20);
out = new TextField (20);
//--以上初始化文字框,长度为20
bb = new Button ("Current Time:");//设置出按钮,
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
//--新建三个panel
setLayout(new GridLayout(3,1));//设置布局使用网格布局
setSize(200,100);//设置该窗口大小为200x100
p1.add(in);//将in文本框添加到p1
p2.add(bb);//将按钮添加到p2
p3.add(out);//将out文本框添加到p3
add(p1);
add(p2);
add(p3);
//---以上将三个panel添加到父容器布局即上面的网格布局
bb.addActionListener(this);//为按钮添加监听器,监听器为实现了ActionListener接口中的方法的本类中定义的监听器
timer = new Thread(this);//实现了Runnable接口,所以将自己作为线程
state = true;//设置状态为true
timer.start();//开始time线程
}
public void actionPerformed (ActionEvent e)
{
//out.setText(in.getText());
out.setText(currentTime());//当按下按钮,设置out文本框的文字为当前时间
}
public void run()
{
//该线程每隔1秒运行一次,目的是为了设置in文本框的时间文字
while (true)
{
try
{
timer.sleep(1000);
}
catch(InterruptedException e){}
in.setText(currentTime());;
}
}
//得到当前时间的方法
String currentTime()
{
Date now = new Date();//新建一个date对象
String str = now.getHours() + ":" +now.getMinutes() + ":" +now.getSeconds();//得到当前小时+分钟+秒数的文字
return str;//返回文字
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询