package Time;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class WindowAndTime
{
private Frame f ;
private TextArea ta;
WindowAndTime()
{
setWindow();
}
public void setWindow()
{
f = new Frame("实时时钟");
f.setSize(170,65);
f.setLocation(300,300);
f.setLayout(new FlowLayout());
f.add(setTextArea());
setWindowEvent();
f.setVisible(true);
}
//文本框设置
public TextArea setTextArea()
{
ta = new TextArea();
ta.setColumns(20);
ta.setRows(2);
return ta;
}
//日期设置
public void setDate()
{
while(true)
{
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
String str = sdf.format(d);
ta.setText(str);
try {
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
//事件监听器
public void setWindowEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
package Time;
public class Test
{
public static void main(String args [])
{
new WindowAndTime().setDate();; //主方法
}
}
|
|