结合SWING和RUNNABLE接口,编写一个简单图形用户界面,显示3个线程时钟,程序要求入下:
框架大小(300,100)布局管理器Gralayout;显示时钟为当前时间,窗口为不关闭时间,时间一直显示,而且要
第1时钟每1秒变化一次,第2时钟每5变化一次,第3时钟每10变化一次
大体步骤好象是:1建一个JAVA类MyClockRanel,
2定义MyClockRanel类为变量
3正确实现MyClockRanel类的构照方法
public MyClockRanel(int sleep time)
{
}
4对MyClockRanel类。正确实现Runnable接口的 run( )方法
用sleep(long milliseamds)方法时参数单位为毫秒,并捕捉InterruptedException异常
5编写主程序类 ThreeClocks实现main()方法
大体框架是
import java.unil.data;
import java.awt.*;
import java.event.*;
class MyClockRanel extends Panel implements Rannable
{
}
class ThreeClocks
{
}
框架大小(300,100)布局管理器Gralayout;显示时钟为当前时间,窗口为不关闭时间,时间一直显示,而且要
第1时钟每1秒变化一次,第2时钟每5变化一次,第3时钟每10变化一次
大体步骤好象是:1建一个JAVA类MyClockRanel,
2定义MyClockRanel类为变量
3正确实现MyClockRanel类的构照方法
public MyClockRanel(int sleep time)
{
}
4对MyClockRanel类。正确实现Runnable接口的 run( )方法
用sleep(long milliseamds)方法时参数单位为毫秒,并捕捉InterruptedException异常
5编写主程序类 ThreeClocks实现main()方法
大体框架是
import java.unil.data;
import java.awt.*;
import java.event.*;
class MyClockRanel extends Panel implements Rannable
{
}
class ThreeClocks
{
}
import java.awt.*;
import java.awt.event.*;
class MyClockRanel extends Panel implements Runnable
{
private TextField tf = new TextField("0",30);
private int interval = 0;
public MyClockRanel(int interval)
{
this.interval = interval;
this.add(tf);
}
public void run()
{
while(true)
{
Date date = new Date();
tf.setText(date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
System.out.println(date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
try
{
Thread.sleep(interval*1000);
}
catch(Exception exp){}
}
}
}class MyFrame extends Frame
{
MyClockRanel cr1 = new MyClockRanel(1);
MyClockRanel cr2 = new MyClockRanel(5);
MyClockRanel cr3 = new MyClockRanel(10);
public MyFrame()
{
this.setLayout(new GridLayout(3,1));
this.add(cr1);
this.add(cr2);
this.add(cr3);
this.dispose();
new Thread(cr1).start();
new Thread(cr2).start();
new Thread(cr3).start();
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
public class Test
{
public static void main(String[] args)
{
MyFrame f = new MyFrame();
f.setVisible(true);
f.setSize(300,100);
}
}
import java.awt.event.*;
import java.util.*;class MyClockRanel extends Panel implements Runnable
{
private Date mydate;
private int myhours,myminutes,myseconds,delay;
private Thread thr;
private TextField text;
public MyClockRanel(int sleeptime)
{
delay = sleeptime;
mydate = new Date();
myhours = mydate.getHours();
myminutes = mydate.getMinutes();
myseconds = mydate.getSeconds();
text = new TextField(myhours + ":" + myminutes + ":" + myseconds);
add(text);
show();
thr = new Thread(this);
thr.start();
//thr.run();
}
public void run()
{
int counter = 0;
int temp;
while(true)
{
mydate = new Date();
temp = mydate.getSeconds();
if(temp >= ((myseconds+1)%60))
{
counter ++;
myhours = mydate.getHours();
myminutes = mydate.getMinutes();
myseconds = mydate.getSeconds();
if(counter >= delay)
{
counter = 0;
text.setText(myhours + ":" + myminutes + ":" + myseconds);
}
}
}
}
}public class ThreeClocks extends Frame
{
public ThreeClocks()
{
super("三个时钟");
MyClockRanel time1 = new MyClockRanel(1);
MyClockRanel time2 = new MyClockRanel(5);
MyClockRanel time3 = new MyClockRanel(10);
setLayout(new GridLayout(1,3));
add(time1);
add(time2);
add(time3);
addWindowListener(new WindowCloser());
pack();show();
}
void init()
{
setSize(300,100);
}
public class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(-1);
}
}
public static void main(String[] args)
{
new ThreeClocks();
}
}
import java.awt.event.*;
import java.util.*;class MyClockRanel extends Panel implements Runnable
{
private Date mydate;
private int myhours,myminutes,myseconds,delay;
private Thread thr;
private TextField text;
public MyClockRanel(int sleeptime)
{
delay = sleeptime;
mydate = new Date();
myhours = mydate.getHours();
myminutes = mydate.getMinutes();
myseconds = mydate.getSeconds();
text = new TextField(myhours + ":" + myminutes + ":" + myseconds);
add(text);
show();
thr = new Thread(this);
thr.start();
//thr.run();
}
public void run()
{
int counter = 0;
int temp;
while(true)
{
mydate = new Date();
temp = mydate.getSeconds();
if(temp >= ((myseconds+1)%60))
{
counter ++;
myhours = mydate.getHours();
myminutes = mydate.getMinutes();
myseconds = mydate.getSeconds();
if(counter >= delay)
{
counter = 0;
text.setText(myhours + ":" + myminutes + ":" + myseconds);
}
}
}
}
}public class ThreeClocks extends Frame
{
public ThreeClocks()
{
super("三个时钟");
MyClockRanel time1 = new MyClockRanel(1);
MyClockRanel time2 = new MyClockRanel(5);
MyClockRanel time3 = new MyClockRanel(10);
setLayout(new GridLayout(1,3));
add(time1);
add(time2);
add(time3);
addWindowListener(new WindowCloser());
pack();show();
}
void init()
{
setSize(300,100);
}
public class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(-1);
}
}
public static void main(String[] args)
{
new ThreeClocks();
}
}